2

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.

Benjamin Collins
  • 1,044
  • 2
  • 9
  • 23

3 Answers3

9

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.

Masstell
  • 91
  • 1
  • 4
0

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

Changed "wb" to "w"

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • 2
    Thanks for the reply! Changed it to "w", and added in chmod(file, 0777); Deleted the file, tried again. File made, still 0 bytes. Tried it one more time to see if it wrote to file. Still 0 bytes, nothing written into the file. – Benjamin Collins Jan 17 '13 at 08:53
  • @Prasanth What would happen if you changed your 'wb' parameter to 'w+', for example? – Tash Pemhiwa Jan 17 '13 at 13:14
  • either 'w' or 'w+', you should be able to write to file. As you are not able to write to it with 'w', I think 'w+' wont make any difference. – Prasanth Bendra Jan 17 '13 at 13:17
  • @PrasanthBendra My suggestion was more about removing the 'b' than anything else really. Does it not have an impact? – Tash Pemhiwa Jan 18 '13 at 05:53
  • Changing it to "w" didn't have any effect. Tried "a" as well, same thing, file made, but empty. – Benjamin Collins Jan 19 '13 at 07:42
  • echo your $unencodedData, and see any data is there and echo fwrite( $fp, $unencodedData); it will give you length of data written in file. – Prasanth Bendra Jan 21 '13 at 06:28
0

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.

Krishna Modi
  • 377
  • 2
  • 12