tags:

views:

346

answers:

7

I'm using @fopen to open a file in "rb" mode. the file that im opening here is running with no errors but if i open that file using @fopen then it is giving error.

code is something like this---

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

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

sometimes it opens up without sending any error mail but sometimes it starts giving so many error mails.

what is the solution to open this url without having any error mail? plz help!!

+1  A: 

If you're trying to open a URL (presuming from the 'xyz.com' you included), then you need to include the schema declaration before it. E.g. http://xyz.com, otherwise PHP will attempt to open a local file. If you're referring to a local file, make sure to escape any back-slashes if you're on Windows.

However, there's nothing else inherently wrong with the rest of your code sample that should cause a problem. @ simply suppresses error outputs, so it won't be causing any odd behaviour in and of itself.

Though, that said, a better way to handle it might be to do this:

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

if(!$file)
{
    mail($to, $subject, $message, $from);
    die();
}
James Burgess
ya you are right James but im using http:// though i havent written that here in my code but im using it but still mails get generated.......i want to get rid of those error mails.....
developer
Have edited my answer to a 'better' code sample... try that, and see if you still get the same problem.
James Burgess
just comment out the mail() function if you want to get rid of the mails :) the reason it sometimes (but not always) fails may be that the target server xyz.com isn't always responding (fast enough). a solution to this problem depends on what you need the app for.
Schnalle
A: 

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

動靜能量
im initializing $flag=0 in the starting of the file.......so everytime it is set to 0 first then if @fopen fails then only it will set to 1..........
developer
A: 

remove the '@' charactor from the start of the fopen method, (ther presence of the @ symbol supresses any php driven error message) this will give you the explanation of why php thinks you cant open that file - i would hazard a guess either the path to the file or the permissions of the file are invalid.

simon622
okk i will do that and will check......will tell if theres any error.....
developer
A: 

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

Is url fopen always allowed in your ini? Maybe this value overrides somewhere with ini_set()?

Are you sure, that url is correct and host is alive?

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

Jet
+1  A: 

Try to use the

file_get_contents();

function instead of fopen().

powtac
A: 

are you trying open a url or a file ... If its a url add http:// to the file name ... and initialize your $flag variable to value zero before fopen() and if its a url fopen() may fail... use file_get_contents() instead of that

Syamsasi
A: 

The @ symbols suppresses errors so $flag will never be set

Matt Williamson