Dragen Posted April 20, 2007 Share Posted April 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/47866-solved-continue-output-after-fopen-error/ Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47866-solved-continue-output-after-fopen-error/#findComment-233861 Share on other sites More sharing options...
Dragen Posted April 20, 2007 Author Share Posted April 20, 2007 that's perfect! don't know why I didn't realise that.. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/47866-solved-continue-output-after-fopen-error/#findComment-233869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.