Error when using @fopen

I am using @fopen to open a file in "rb" mode. the file that opens here runs without error, but if I open that file with @fopen it throws an error.

the code looks something like this:

$file = @fopen("xyz.com","rb") or $flag=1;

if($flag==1)
{
    mail($to, $subject, $message, $from);
    die();
}

      

sometimes it opens without sending any error mail, but sometimes it starts giving so many error emails.

what is the solution to open this url without any errors? Help plz !!

0


a source to share


6 answers


If you are trying to open a URL (assuming you included "xyz.com"), you need to include the schema declaration in it. For instance. http://xyz.com , otherwise PHP will try to open the local file. If you are referring to a local file, make sure you remove any backslashes if you are on Windows.

However, nothing else is wrong with the rest of your sample code that should be causing the problem. @ just suppresses error outputs, so by itself it doesn't cause any odd behavior.



Although, as said, the best way to deal with this might be:

$file = @fopen("xyz.com","rb");

if(!$file)
{
    mail($to, $subject, $message, $from);
    die();
}

      

+1


a source


Try to use

file_get_contents(); 

      



instead of the fopen () function.

+1


a source


by the way, you are setting $ flag = 1 when an error occurs. but what if the last time there was a mistake, and this time there is no mistake? (then the $ flag is still 1 from the previous time).

0


a source


remove the "@" attribute from the beginning of the fopen method (the presence of the @ symbol suppresses any php error message), this will give you an explanation as to why php thinks you cannot open this file - I would risk assuming the file path or file permissions are invalid.

0


a source


What is the error message? We can just guess the problem without her.

Is url fopen always resolved in your ini? Maybe this value is overridden somewhere using ini_set ()?

Are you sure the correct url and host is alive?

Finally, I recommend using fsockopen instead. It provides more flexible remote connections, error handling for them, and the ability to set a connection timeout.

0


a source


@ Symbols suppress errors, so the $ flag will never be set

0


a source







All Articles