fopen()的“failed to open stream”错误

一、出现问题

编辑1.php文件,如下:

<?php
$filename='1.txt';
$handle=fopen ($filename, "w");

fwrite ($handle, "<b>i like dancing very much!</b>\r\n");
fwrite ($handle, "i like dancing very much!\r\n");
fclose ($handle);
echo "the function of fgets's results is:";
$handle=fopen("1.txt","r");

while (!feof($handle)){
    $buffer=fgets ($handle,1024);
    echo $buffer;
}
fclose ($handle);
echo "<br><br>the function of fgetss's output is:";

?>

运行报错:

Warning:  fopen(mytest/1.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream

Warning:  fgets() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/mytest/1.php

Warning:  feof() expects parameter 1 to be resource, boolean given in /opt/lampp/htdocs/mytest/1.php on line 10

二、网上众说纷纭,

1、针对failed to open stream ,有的报错是:Warning:   fopen(text.txt)   [function.fopen]:   failed   to   open   stream:   Permission   denied   in   (.....path...)write.php   on   line   11

即没有权限修改这个文件,把文件改为可写,给文件权限即可。所以建议chmod 777 文件。

2、php.ini  中allow_url_fopen =on(默认即是on)

3、查询“……xpects parameter 1 to be resource, boolean given in……”网上因为好多是设计的mysql查询的,都是建议检查查询语句。


三、最后解决,发现fopen(mytest/1.txt)路径有问题,1.txt权限也未开。将两者修改后,不再报错。

****************

注:

http://wenku.baidu.com/view/b22caa2f7375a417866f8f19.html中提到如下一段信 息:

如何处理PHP的错误信息

如果错误信息中包含了服务器的信息,我们就会需要使用php中的字符@隐藏这条函数代码。

例如:

<?php

$fp=fopen("www.cycf.org.cn","r");

fclose($fp);

?>

当然,错误如下:

Warning:fopen() expects at least 2 parameters, I given in E:\wampp\cycf\php\test.php on line2

Warning:fclose() expects parameter 1 to be resource, boolean given in E:\xampp\htdocs\php\Test\9.4.1.php on line3

如果我们是在服务器上出错,这样输出错误信息就会暴露服务器的路径。就要想到,如何去隐藏这个路径。

打开php的报错功能:

修改php.ini文件,将display_errors=off改成on,然后重启web服务。

例如:

<?php

$fp=@fopen("haha.txt","r");

@fclose($fp);

?>

一个@屏蔽一条语句的错误信息,可以发现什么都木有。

定制错误的信息

“@”虽然可以隐藏信息,但是这样不利于用户的友好体验,因为加了这条php代码之后,phpdo的当前状态也被屏蔽了。

 

你可能感兴趣的:(PHP,职场,休闲,fopen())