title: PHP using include_path with filesize to avoid Warning: filesize(): stat failed for
body: <p>In php, I know I can use <code>include_path</code> with <code>fopen</code>, but I don't see a way to use it with <code>filesize</code>. The following code results in an error message...</p>
  <pre><code>ini_set('include_path', '.:/home/user/lib:../../../lib');
  
  const MY_FILE = 'myfile.txt';
  
  $myFile = fopen(MY_FILE, &quot;r&quot;, true) or die(&quot;Unable to open file!&quot;);
  $myFileSize = filesize(MY_FILE);
  $myFileContent = fread($myFile, $myFileSize);
  echo $myFileContent;
  fclose($myFile);
  
  </code></pre>
  <p>The error...</p>
  <pre><code>Warning: filesize(): stat failed for myfile.txt in /Volumes/...
  </code></pre>
  <p>The edit below works, but I don't like having to set the same paths manually and redundantly.</p>
  <pre><code>ini_set('include_path', '.:/home/user/lib:../../../lib');
  
  const MY_FILE = 'myfile.txt';
  
  $myFile = fopen(MY_FILE, &quot;r&quot;, true) or die(&quot;Unable to open file!&quot;);
  if (file_exists(&quot;/home/user/lib/&quot; . MY_FILE)) {
    $myFileSize = filesize(&quot;/home/user/lib/&quot; . MY_FILE);
  } elseif (file_exists(&quot;../../../lib/&quot; . MY_FILE)) {
    $myFileSize = filesize(&quot;../../../lib/&quot; . MY_FILE);
  }
  $myFileContent = fread($myFile, $myFileSize);
  echo $myFileContent;
  fclose($myFile);
  </code></pre>
  <p>Suggestions for improvement?</p>
  <p>Option 1) Implementing suggestion by @kmoser...</p>
  <pre><code>ini_set('include_path', '.:/home/user/lib:../../../lib');
  $paths = explode(&quot;:&quot;, get_include_path());
  
  const MY_FILE = 'myfile.txt';
  
  $myFile = fopen(MY_FILE, &quot;r&quot;, true) or die(&quot;Unable to open file!&quot;);
  foreach ($paths as $value) {
    if (file_exists($value . &quot;/&quot; . MY_FILE)) {
      $myFileSize = filesize($value . &quot;/&quot; . MY_FILE);
      break; // Stop searching after we find first matching file
    }
  }
  $myFileContent = fread($myFile, $myFileSize);
  echo $myFileContent;
  fclose($myFile);
  </code></pre>
  <p>Bonus: I like this version suggested by @AlexHowansky in case we simply want to read the file...</p>
  <pre><code>ini_set('include_path', '.:/home/user/lib:../../../lib');
  
  const MY_FILE = 'myfile.txt';
  
  $myFileContent = get_file_contents(MY_FILE, true);
  echo $myFileContent;
  
  </code></pre>