fopen()的“failed to open stream”错误
2011-10-26 13:10
387 查看
一、出现问题
编辑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的当前状态也被屏蔽了。
》
编辑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 写入文件时抛failed to open stream:Permission denied in 错误的解决方法
- php文件上传 [function.move-uploaded-file]: failed to open stream 错误解决办法
- fopen: failed to open stream: Permission de
- Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire
- 错误: failed to open stream: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
- Yii2访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire
- 使用solr报错,错误信息 include(SolrClient.php): failed to open stream: No such file or directory
- PHP使用copy函数复制windows文件时出现failed to open stream: Invalid argument in错误
- 关于php使用fopen报错 failed to open stream: Permission denied
- Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire
- file_put_contents 错误:failed to open stream: Invalid argument 一种原因
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- PHP "Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0" 错误
- php上传文件时出现错误:failed to open stream: Permission denied
- Lamp环境下:fopen failed to open stream:permission denied
- [ErrorException]fopen(./data/data.csv): failed to open stream: No such file or directory
- [function.fopen]: failed to open stream
- linux下PHP项目写入日志失败,fopen(xxx.log): failed to open stream: Permission denied
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- Warning: fopen(/var/www/costway.com/var/locks/index_process_3.lock): failed to open stream: Permissi