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?

3
  • Have you checked that your php version really bails out on a warning? Try a new script that only contains <?php fopen('doesnotexist', 'rb'); echo 'if you can read this...'; Commented Nov 26, 2009 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 stackoverflow.com/questions/10520390/…, 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. Commented Feb 19, 2016 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? Commented Mar 3, 2016 at 6:54

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

is_readable and is_writable are horribly broken on Windows systems, and I think on *nix with ACL's too.
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.

1 Comment

It can also be a special configuration setting. See my comment to the question.
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.

Comments

1

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

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

Comments

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.

Comments

-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
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.