error on using @fopen

2019-08-29 01:51发布

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!!

标签: php fopen
6条回答
够拽才男人
2楼-- · 2019-08-29 02:15

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.

查看更多
该账号已被封号
3楼-- · 2019-08-29 02:17

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();
}
查看更多
小情绪 Triste *
4楼-- · 2019-08-29 02:18

Try to use the

file_get_contents(); 

function instead of fopen().

查看更多
Animai°情兽
5楼-- · 2019-08-29 02:18

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

查看更多
smile是对你的礼貌
6楼-- · 2019-08-29 02:23

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

查看更多
看我几分像从前
7楼-- · 2019-08-29 02:24

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.

查看更多
登录 后发表回答