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 !!
a source to share
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();
}
a source to share
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.
a source to share
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.
a source to share