4

I have a PHP script that stops processing (it seems to be) on a PHP warning. The error is general "PHP Warning: fopen" - "failed to open stream: No such file or directory".

Should PHP stop here? I thought only on fatal errors?

Is there a way to get it to continue?

Abs
  • 56,052
  • 101
  • 275
  • 409
  • Have you checked that your php version really bails out on a warning? Try a new script that only contains ` – VolkerK Nov 26 '09 at 10:00
  • The OP is right that PHP is not expected to stop on a reported or displayed warning. The only configuration, besides using a special handler, see http://stackoverflow.com/questions/10520390/stop-script-execution-upon-notice-warning, that I am aware of that can do that is with Xdebug. If you have the xdebug module installed and `xdebug.halt_level` includes `E_WARNING`, then PHP will halt on system warnings. Sure, we want to remove warnings, but still the system should not halt on them, unless we want that. So, the answer is that the OP should check his configuration settings. –  Feb 19 '16 at 23:15
  • I am having the same issue on a fresh install; PHP pre-processes the script and when it hits notices it dumps them to the output buffer but apparently doesn't execute any code? –  Mar 03 '16 at 06:54

6 Answers6

2

Don't know how to continue on errors, but the better thing would be error prevention in first place:

http://php.net/manual/en/function.file-exists.php

http://www.php.net/manual/en/function.is-readable.php

Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
2

A warning is emitted but the script execution continues. The fact that your script stops is more likely related to processing you try to do afterward but not to the warning itself.

The previous suggestion to use file_exists and is_readable is a good one.

Arnaud
  • 101
  • 5
  • It can also be a special configuration setting. See my comment to the question. –  Feb 19 '16 at 23:23
1

Yes, it should if error_reporting() level is low enough.

Yes, there is. Add "@" before fopen which causes the waring, like this: @fopen(...)

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60
1

As noted on the php documentation page,

If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning.

Conrad Meyer
  • 2,851
  • 21
  • 24
0

In addition to what Conrad Meyer has mentioned from the PHP manual:

$fres = @fopen('file.ext','w+');
if($fres){
  // so anything you want with the file
}

fopen returns false on error. When there's an error suppressed on fopen and you do not use the if($fres), the subsequent file operation functions will throw error saying that $fres is not a valid file handle.

mauris
  • 42,982
  • 15
  • 99
  • 131
-1

Even if it continued, the program would, most probably, not work the way it was meant to. Anyway, try handling the exception:

try {
    # code that may cause an error
}
catch( Exception $e ) {
    # do error handling mechanism
}
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • Actually the script should continue and can do without that file - I haven't written it well yet but I just want it to continue if it doesn't find certain files – Abs Nov 26 '09 at 09:54