无法打开文件进行写入
我正在尝试写入文件。在执行 fopen
之前,我对其进行了 file_exists
检查,它返回 true
(该文件确实存在)。
但是,该文件失败了此代码,并且每次都会给出错误:
$handle = fopen($filename, 'w');
if($handle)
{
flock($handle, LOCK_EX);
fwrite($handle, $contents);
}
else
{
echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
exit();
}
flock($handle, LOCK_UN);
fclose($handle);
有没有办法获得更具体的错误详细信息,以了解为什么该文件不允许我打开它进行写入?我知道该文件名是合法的,但由于某种原因它不允许我写入它。我确实有写权限,我能够写入并覆盖另一个文件。
I am trying to write to a file. I do a file_exists
check on it before I do fopen
and it returns true
(the file does exist).
However, the file fails this code and gives me the error every time:
$handle = fopen($filename, 'w');
if($handle)
{
flock($handle, LOCK_EX);
fwrite($handle, $contents);
}
else
{
echo 'ERROR: Unable to open the file for writing.',PHP_EOL;
exit();
}
flock($handle, LOCK_UN);
fclose($handle);
Is there a way I can get more specific error details as to why this file does not let me open it for writing? I know that the filename is legit, but for some reason it just wont let me write to it. I do have write permissions, I was able to write and write over another file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文件存在并不意味着您有权写入该文件。在尝试写入文件之前,您应该使用
is_writable
。Just because the file exists doesn't mean that you have permission to write to it. Before trying to write to a file, you should check to see if PHP has permission to do so using
is_writable
.或获取错误的一般方法:
查看实际的错误消息
or general way of getting errors:
to see the actual error message
如果您使用的是 php 5.2+,您可能会对 error_get_last() 感兴趣。
在您的开发系统上,您还可以通过 error_reporting() 在脚本中提高错误报告级别,或者(最好是)在你的 php.ini 中。
If you're using php 5.2+ you might be interested in error_get_last().
On your development system you can also increase the error reporting level, either within the script via error_reporting() or (preferably) in your php.ini.
听起来您正在写入的目录没有正确的权限。
Sounds like the directory you're writing to doesn't have the correct permissions.