PHP fwrite()无法正常工作 [英] PHP fwrite() not working

查看:397
本文介绍了PHP fwrite()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用php编写一个函数,在客户端,我有一个画布图像,该图像使用toDataUrl()以及文件名将图像保存在服务器上.这是代码:

I'm writing a function in php, client side I have a canvas image which I use toDataUrl() along with a file name to save the image on the server. The here's the code:

<?php
  $imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
  $data = json_decode($imageData, true);

  $file = $data["file"];
  $image = $data["data"];

  $filteredData=substr($image, strpos($image, ",")+1);
  $unencodedData=base64_decode($filteredData);

  $fp = fopen( 'image/' . $file , 'wb' );
  fwrite( $fp, $unencodedData);
  fclose( $fp );
?>

问题是此代码有效.在我使用过的三分之二的页面中,它工作正常.问题是当我第三次复制并粘贴它以再次实现它时,由于某种原因,文件是在服务器上制作的,除了没有数据写入文件外.我不认为这是客户端的问题,因为我在javascript中编写了调试警报消息,并在PHP中编写了调试回显,并且两者都可以很好地打印出数据.我制作了这个简短的调试文件:

The thing is that this code works. And for two out of three of the pages I used it on it works fine. The problem is when I copy and pasted it a third time to implement it again, for some reason the file is made on the server except that no data get's written into the file. I don't think it's a problem client side because I write in a debug alert message in the javascript and a debug echo into the PHP and both are able to print out the data fine. I made this short debug file:

<?php
$fp = fopen('data.txt', 'wb');
if(is_writable('data.txt')){
    echo "file is writable<br>";
}
if(fwrite($fp, 'test') == FALSE){
    echo "failed to write data<br>";
}
fclose($fp);
?>

输出为

file is writable
failed to write data

在尝试将chmod设置为0777之前,我尝试使用chmod并将所有内容,文件夹,文本文件设置为0777,但仍然得到相同的结果;文件已创建,但没有数据写入其中.有什么我想念的东西或其他可能有用的方法吗?我还没有在Google上找到任何东西,对于为什么相同的代码在没有明显原因突然停止之前为何能按预期两次正常工作仍然感到困惑.

I've tried using chmod and setting everything, the folder, the text file before I write to it to 0777 and I still get the same result; the file is made but no data is written into it. Is there anything I'm missing or any other approaches that might help. I haven't found anything on google and am still baffled as to why the same code worked exactly as expected twice before suddenly stopping for no apparent reason.

先谢谢了.

推荐答案

我知道这是一篇旧文章,但是我遇到了一个非常相似的问题,并且找到了解决方案(至少对我来说)!我的服务器磁盘空间不足,因此它可以创建一个0字节的文件,但不会写入该文件.在我清理了一些空间(删除了一个13gb的error.log文件)后,所有内容又开始按预期方式工作.

I know this is an old post, but I had a very similar problem and found a solution (for me at least)! I ran out of disk space on my server, so it could create a 0 byte file, but wouldn't write to it. After I cleared out some space (deleted a 13gb error.log file) everything started working again as expected.

如果fopen有效,但fwrite神秘地无效,请检查您的磁盘空间. "df -h"是用于检查Linux服务器上磁盘空间的命令.

If fopen works but fwrite mysteriously doesn't, check your disk space. 'df -h' is the command to check disk space on a linux server.

这篇关于PHP fwrite()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆