nysteve Posted October 8, 2007 Share Posted October 8, 2007 Noob here.. So I'm learning from w3c and doing some samples but i'm having an issue: code: -------------------- $file = fopen("text.txt","r") or exit("file not found biatch"); while(!feof($file)) { echo fgets($file)." "; } fclose($file); ------------------- all source files located in my: 'C:\wamp\www' folder. but when i try to force error aka change 'text.txt' to 'tex2.txt' i get a this warning: Warning: fopen(tyext.txt) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\test.php on line 35 and my exit message. why? i know this is a split second q.. but noobs have to start somewhere lol. steve :-\ Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/ Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 That is because you code is saying to find text.txt NOT tex2.txt Change it to this, and it should work. <?php $file = fopen("tex2.txt","r") or exit("file not found biatch"); while(!feof($file)) { echo fgets($file)." "; } fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364891 Share on other sites More sharing options...
nysteve Posted October 8, 2007 Author Share Posted October 8, 2007 the reason i changed the file name is so that i could get the error exit message. i was under the impression that if the file name was not found.. the exit message would show up and that's it. am i missing something? Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364905 Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 instead of or exit, try or die then maybe you will get only the message you were expecting and not the error. Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364910 Share on other sites More sharing options...
nysteve Posted October 8, 2007 Author Share Posted October 8, 2007 did not fix it. maybe i need to start with the basic question; - what is exit used for? i thought that it would exit the script if an error occurred, and a file not found is a type of error is it not? Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364914 Share on other sites More sharing options...
The Little Guy Posted October 8, 2007 Share Posted October 8, 2007 Yes, that should be the case. It is possible that your php file is set up to show that error message along with your message. Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364918 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 If you don't want the PHP error message to appear, but you do want your error to appear, use the "@" symbol before the function. This symbol suppresses the error message. <?php $file = @fopen("text.txt","r") or die("file not found biatch"); while(!feof($file)) { echo fgets($file)." "; } fclose($file); ?> BTW, "or die" and "or exit" are the same. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364926 Share on other sites More sharing options...
nysteve Posted October 8, 2007 Author Share Posted October 8, 2007 that worked! thanks. question still remains; what need is their for an exit in this example (as shown on w3s) if both the exit message and the php error appears at the same time..hmm and what does the "@" represent/how is it used (i did not find a ref in the php manual). thanks knerbnsn Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364945 Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 If you don't have the exit() or die() statement in your script, it will continue and you will get more errors. This is because the original error is only a warning. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-365006 Share on other sites More sharing options...
lessthanthree Posted October 8, 2007 Share Posted October 8, 2007 To add to Ken's statement....If your PHP ini was set to not display warnings (as it should be on a production/live server) then you would not see the warning message. The '@' symbol in front of a function is used to supress any warnings that the function may throw on it's running. As it's only a warning (as opposed to a 'fatal error')the script would continue to run and as ken said, display any further warnings / errors that your script may have. The exit/die statement will kill the script exactly where it is, thus resulting in no further errors being displayed. Hope that clears it up a bit. Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-365010 Share on other sites More sharing options...
nysteve Posted October 9, 2007 Author Share Posted October 9, 2007 thanks for the info guys! Quote Link to comment https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-365056 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.