doujiao1538
2016-10-04 15:18
浏览 350

fopen创建文件,为什么fwrite失败?

I have this stupid little test PHP script running on a Ubuntu system inside an instance of a virtual server (Oracle Virtual Box) running on my pc:

<?
error_reporting(E_ALL);
ini_set('display_errors', 1); // show errors

echo "<p>test</p>";

$filename = "andy.txt";
$fh = fopen($filename, 'w')  or die('fopen failed');
fwrite($fh, "qwerty")  or die('fwrite failed');
fclose($fh);
?>

Despite all appropriate directory and file permissions being set, it is failing on the fwrite. The fopen works and creates the file, so write access is clearly enabled, but the fwrite dies, and the 'fwrite failed' message is output (no other error output is displayed).

The same script works perfectly well when I upload to my real server, so I am completely stumped as to why it won't write to the file; maybe it's something about my virtual server that is causing the problem.

Seems like such a pathetic thing, but it's driving me nuts! Considerable time Googling has failed to yield an answer, so can anybody here please provide some insight? Many thanks.

图片转代码服务由CSDN问答提供 功能建议

我在虚拟服务器实例内的Ubuntu系统上运行这个愚蠢的小测试PHP脚本(Oracle Virtual Box) )在我的电脑上运行:

 &lt;?
error_reporting(E_ALL); 
ini_set('display_errors',1);  //显示错误
 
echo“&lt; p&gt; test&lt; / p&gt;”; 
 
 $ filename =“andy.txt”; 
 $ fh = fopen($ filename,'w')或die(  'fopen failed'); 
 nfwrite($ fh,“qwerty”)或die('fwrite failed'); 
 nclclose($ fh); 
?&gt; 
   
 \  n 

尽管设置了所有适当的目录和文件权限,但它在fwrite上失败了。 fopen工作并创建文件,因此显式启用了写访问,但fwrite死了,输出'fwrite failed'消息(不显示其他错误输出)。

当我上传到我的真实服务器时,相同的脚本运行得非常好,所以我完全不知道为什么它不会写入文件; 也许这是我的虚拟服务器引起问题的原因。

看起来像是一件可悲的事情,但它让我疯了! 相当长的时间谷歌搜索未能给出答案,所以请问这里的任何人提供一些见解吗? 非常感谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumu4032 2016-10-04 15:26

    Not sure why the fwrite() call would die, as it returns the number of bytes written.

    That said, have you tried with file_put_contents() instead? It's a simpler way of writing to a file, and is the recommended way since early PHP 5.
    With it you only need to do the following

    $filename = "andy.txt";
    if(!file_put_contents ($contents, $filename)) {
        // Write failed!
    }
    

    No need to bother with opening and closing the file pointer, as that's automatically handled by the function. :)

    评论
    解决 无用
    打赏 举报
  • dongzhuo1958 2016-10-05 11:41

    Solved! It was a disk space error on my virtual server. At the back of my mind, I knew I had seen this mentioned elsewhere as an issue with write fails, but in this case I failed to make the connection.

    @ChristianF Thanks! Switching to file_put_contents() was very helpful, since it also failed, but gave me a meaningful error message:

    'file_put_contents(): Only 0 of 6 bytes written, possibly out of free disk space'
    

    Aha! Having recalled that growing log files can be a problem, I took it upon myself to delete everything inside /var/log (after saving them) and Presto! it now works! So, thank you for that tip - I will switch to using file_put_contents from now on. BTW: The contents of error.log itself was 2GB, while the remaining size of everything else in /var/log was only about 15MB, but deleting error.log by itself did not work, so I deleted everything.

    @Clayton Smith Thank you, but removing the "or die('fwrite failed')" part did not result in any further error info - which is what is so frustrating: It's a shame that those error reporting directives at the start of the script didn't seem to do much.

    @NaeiKinDus Thank you, but I don't think I have SELinux running (I'm afraid I don't know anything about this). Although I have a /etc/selinux directory present, there's no config file in it, just what appears to be a skeleton semanage.conf - whatever that is. Commands such as sestatus are not recognised.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题