title: PHP Get Basename of File Where Function Called
body: <p>Anyone can help me get the basename of directory where the function called? I mean:</p>
  
  <p>file /root/system/file_class.php</p>
  
  <pre><code>function find_file($dir, $file) {
      $all_file = scandir($dir);
      ....
  }
  
  function does_exist($file) {
      $pathinfo = pathinfo($file);
      $find = find_file($pathinfo["dirname"], $pathinfo["basename"]);
      return $find;
  }
  </code></pre>
  
  <p>file /root/app/test.php</p>
  
  <pre><code>$is_exist = does_exist("config.php");
  </code></pre>
  
  <p>Under /root/app i have file "config.php, system.php". Do you know how to get the directory where <code>does_exist()</code> called? In function <code>find_file()</code> argument <code>$dir</code> is important, since <code>scandir()</code> function need directory path to scaned. I mean, when i want to check file <code>config.php</code> i doesn't need to write <code>/root/app/config.php</code>. If i not provide fullpath in <code>$file</code> argument, the $pathinfo["dirname"] will be <code>"."</code>. I've try to use <code>dirname(__file__)</code> in <code>file_find()</code> function but it's return the directory <code>/root/system</code> not <code>/root/app</code> where it is the directory of <code>does_exist()</code> function called.</p>
  
  <p>I need create those function since i can't use <code>file_exists()</code> function.</p>
  
  <p><strong>Found Solutions</strong>:</p>
  
  <p>I'm using <code>debug_backtrace()</code> to get the recent file and line number of where users calling function. For example:</p>
  
  <pre><code>function read_text($file = "") {
      if (!$file) {
          $last_debug = next(debug_backtrace());
          echo "Unable to call 'read_text()' in ".$last_debug['file']." at line ".$last_debug['line'].".";
      }
  }
  </code></pre>
  
  <p>/home/index.php</p>
  
  <p><code>16  $text = read_text();</code></p>
  
  <p>The sample output: <code>Unable to call 'read_text()' in /home/index.php at line 16.</code></p>
  
  <p>Thanks.</p>