PHP fwrite() not working

心已入冬 提交于 2019-12-08 03:54:28

问题


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 );
?>

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);
?>

And the output is

file is writable
failed to write data

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.

Thanks in advance.


回答1:


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.

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.




回答2:


instead of $fp = fopen('data.txt', 'wb'); give $fp = fopen('data.txt', 'w'); and try

Changed "wb" to "w"




回答3:


When you write $fp = fopen('data.txt', 'w'); for your domain website.com having root at /var/www/website/ and if the php file is located at /var/www/website/php/server/file/admin.php or something similar, it will actually create a file at /var/www/website/data.txt

Try giving absolute path or path relative to your domain root to create files like,

$fp = fopen('php/server/file/data.txt', 'w');

Try the find command to see if the file is created anywhere else in the folder directory by using the following in Ubuntu,

find /var/www/website/ -name 'data.txt'

I had this issue, probably can help you solve if you have similar issue.



来源:https://stackoverflow.com/questions/14374258/php-fwrite-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!