7

I'm having trouble writing to a file even though is_writable() returns true. Of course, the file exists and is apparently readable. Here is the code:

$file = "data";
echo file_get_contents($file)."<br>";
echo is_writable($file) ? "is writable<br>" : "not writable<br>";
if (file_put_contents($file, "ghijkl", FILE_APPEND) === FALSE) echo "failed<br>";
echo file_get_contents($file)."<br>";

And here is the output:

abcdef
is writable
failed
abcdef
  • looks like duplicate of http://stackoverflow.com/questions/4917811/file-put-contents-permission-denied – naiquevin Jun 28 '12 at 06:04
  • first thing that comes to my mind after reading http://se.php.net/manual/en/function.is-writable.php is ther row: "Safe mode limitations are not taken into account." 2nd thougt is "Other problems could by" no space left on device? – Puggan Se Jun 28 '12 at 06:12
  • There's definitely enough space because I can type `ghijkl` into the file manually. I guess the next step is to ask my admin about permissions then? –  Jun 28 '12 at 06:52
  • what does exec `("ghijkl>> data");` do ? Juat as a test dont keep it there longer than you need – exussum Dec 13 '13 at 16:39

3 Answers3

1

So just need check free space in filesystem in my case i typing in console:
df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/da0s1f 4.3G 4.0G -68M 102% /usr

free up space and function file_put_contents is working after that

Michail M.
  • 735
  • 5
  • 11
0

Did you check the ownership of the file? It might be the reason, try to run this code:

chown username:groupname your_file.txt

Remember to change the username:groupname. Hope it works for you!

joaobarbosa
  • 630
  • 5
  • 13
-1

You have to use this:

$file=realpath('data');

Then you have to use this above $file in file put contents and last one try to give with file type like (filename.filetype).

file_put_contents($file,'fgdfgfg');
Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
Muthukrishna C
  • 147
  • 1
  • 13