0

I have an intermittent issue with the error include failed to open stream ...

99.9% of the time the includes works perfectly, but at peak times of the day I notice that I start getting an error appear in my php error log.

PHP Warning: include ('database.php') failed to open stream: No such file or directory

I only have one file with this include in it so I know full well its works, we have quite a few thousand requests a day with no issues, but out of these thousands of requests we seem to get around 10 a day which simply fail with the error PHP Warning: include ('database.php') failed to open stream: No such file or directory

It has driven me mad! - I have done some digging around to see what else is being requested from the IIS server at the same time and I can see that I am calling a separate php file which is unrelated to the file above. It simply counts the lines within my php error log and if there is an issue it will send an email....

I am wondering if for some reason there is some sort of file process limit that means if the error log is big enough the below code is doing something to PHP to stop it completeing file processes elsewhere such as includes ...

The basis of this code is :-

function getLines($file)
{
    $f = fopen($file, 'rb');
    $lines = 0;

    while (!feof($f)) {
        $lines += substr_count(fread($f, 8192), "\n");
    }

    fclose($f);

    return $lines;
}

$phpcount = getLines("c:/windows/temp/php-errors.log");

I am wondering if for some reason there is some sort of file process limit that means if the error log is too large the above code struggles and PHP stops completing file processes elsewhere thus causing my includes problem ...

Community
  • 1
  • 1
MarkB
  • 123
  • 1
  • 7
  • what's your hard drive usage at during these peak times – skrilled Jun 02 '16 at 23:36
  • I have just compared the hard drive utilisation compared to when the errors occurred. It ranges between 7% and 17% . There were a few peak elsewhere but over the last week nothing has exceeded 40% – MarkB Jun 03 '16 at 08:14
  • 1
    Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Jun 12 '16 at 10:15
  • can you post the full error message ? meaning, which file is triggering it ? – Vic Seedoubleyew Jun 12 '16 at 10:16
  • Something to check? The path in the code that does the include. i.e. `include ('database.php')`. Ensure that the path is an absolute filepath? i.e. `__DIR__ .'/dtabase.php';`. If is is a relative path then something may be changing the 'current working directory' occasionally. Also, `$file` used in that file should be an absolute filepath for the same reason. i.e imo, never use relative filepaths in php. note: You can use relative directories as part of an absolute filepath. – Ryan Vincent Jun 12 '16 at 11:17

1 Answers1

1

My guess is that the error comes from an include in the file that gets called only now and then. It could be the file that counts the log file lines, or the file that sends an email.

According to this checklist, the error seems to be that the problematic file is trying to include database.php using a relative path, and that fails because the current working directory is not the same.

The solution, as mentioned in the checklist, is to use absolute paths instead.

In any case, you should always use absolute paths in your includes. It is very likely to solve the problem.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76