Jump to content

Recommended Posts

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 :-\

 

 

Link to comment
https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364891
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364926
Share on other sites

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 ;D

Link to comment
https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-364945
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72357-solved-fopen-and-exit/#findComment-365010
Share on other sites

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.