views:

74

answers:

6

Hello all,

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?

+2  A: 

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

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

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

Ivan Krechetov
+1  A: 

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
A: 

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
only in PHP 5...
thephpdeveloper
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
A: 

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.

thephpdeveloper
A: 

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