Jump to content

Recommended Posts

Hi,

I've got this script which opens a txt file, taking the directory from variables.

<?php
			$file = "downloads/" . $category . "_" . $item . ".txt"; // opens the txt file for the item. Then sets description as the text file.
			$fh = fopen($file, 'r') or die("Error: Can't find description");
			$description = fread($fh, filesize($file)); // no need for addslashes as data is read as html not php
			fclose($fh);
			echo $description; 
?>

 

If it can't open the file it brings up the error:

Warning: fopen(downloads/games_romasantah.txt): failed to open stream: No such file or directory in /home/fhlinux181/g/gimppro.co.uk/user/htdocs/dwnviewitm.php on line 44
Error: Can't find description

outputting the warning then my error message, which is fine.

The problem is, because I'm using or die(), it doesn't output any of my code past the open file section, meaning that anything else I wish to display below is not shown.

If I remove the or die, it brings up the same error and several more, because it can't find the other variables etc.

 

Is there a way I can write this so that it simply displays the error, but continues after the 'fclose();'?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/47866-solved-continue-output-after-fopen-error/
Share on other sites

this should suffice :-)

<?php
$file="downloads/" . $category . "_" . $item . ".txt";
$fh=@fopen($file, 'r');
if(!$fh) echo 'Error: Can't find description';
else{
$description = fread($fh, filesize($file));
fclose($fh);
echo $description; 
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.