error on using @fopen

為{幸葍}努か 提交于 2020-01-16 19:06:06

问题


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:


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();
}



回答2:


Try to use the

file_get_contents(); 

function instead of fopen().




回答3:


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).




回答4:


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.




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/887013/error-on-using-fopen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!