tags:

views:

45

answers:

4

I'm using fopen to read from a file

$fh = fopen($path, 'r') or die('Could not open file');

Now I contantly get error Could not open file. I checked the file path and even changed the permissions of the file to 777. Is there a way I can get a detailed error report as why the file can't be opened similar to mysql_error()?

+3  A: 

Yes.
PHP has detailed error message for you.
You just have to turn it on.

To dislay it on the screen add these 2 lines at the top of the script:

ini_set('display_errors',1);
error_reporting(E_ALL);

Or if you want it to be logged instead,

ini_set('log_errors',1);
ini_set('display_errors',0);
error_reporting(E_ALL);

Also note that using die() is very bad practice.

Col. Shrapnel
+3  A: 

Turn on error reporting, or, in a production environment (from PHP 5.2.0 onwards) you should also be able to use get_last_error().

Pekka
oh, get\_last\_error() ...that's better than $php\_errormsg. Deleting my answer in favor of this function.
VolkerK
No need to delete it. He may be on a project (like I am currently) that has only 5.1!
webbiedave
convinced......
VolkerK
+3  A: 

For php versions prior to 5.2 (lacking error_get_last()) you can use track_errors.

ini_set('track_errors', 1);
$fh = fopen('lalala', 'r');
if ( !$fh ) {
  echo 'fopen failed. reason: ', $php_errormsg;
}

see also: http://de.php.net/reserved.variables.phperrormsg

VolkerK
+1 pre 5.2 installations are still around. Nice, didn't know this!
Pekka
+1 again - didn't know about the track_errors ini var, that's handy :-)
richsage
A: 

Սաղ ասեցիք այ ախպեր:DDD

Syom
it's armenian!!! i'm just joking;)))
Syom