Read file without php error messages

If you’re using php to read the contents of various files in order to construct a web page, you don’t want a missing file to result in error messages all over the web page. Use the error control operator and a conditional to prevent this happening:

$filename = 'thefile';
if ($file = @fopen($filename, 'r')) {
  while(!feof($file)) {
    echo fgets($file);
  }
  fclose($file);
}
else {
  echo 'Unable to open '.$filename;
}

Last modified: 09/09/2008 Tags:

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top