0

I have checked this massage board three times over looking for the answer.

In PHP, why won't this write to test.txt?

file_put_contents not working

file_put_contents not creating txt file

And yes I have checked, checked and recheck file and folder permissions.

-rw-rw-rw- 1 www-data www-data 3 Mar 14 16:01 fileMsgid.txt

I have tried 777 775 and 650 with no success.

The line of code is simple

    $msgId = 3;   
    file_put_contents('fileMsgid.txt', $msgId); 

The Ubuntu system won't create the file or write content to the file.

This works fine on the Windows development server but when I move the code to the Linux production server, squat happens.

Any ideas?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user1794918
  • 1,131
  • 2
  • 16
  • 34
  • What happens if you write the absolute path of the file? – Shikiryu Mar 14 '16 at 22:35
  • Do you have any error messages? – newman Mar 14 '16 at 22:38
  • what's the output of var_dump(file_put_contents('fileMsgid.txt', $msgId)); ? – the_nuts Mar 14 '16 at 22:39
  • You can try with `__DIR__ . '/fileMsgid.txt'` instead – the_nuts Mar 14 '16 at 22:40
  • Check please PHP version on your ubuntu server. Functions file_put_contents avaiable from version 5. It it very strange, but may be you use PHP 4 on your server. – newman Mar 14 '16 at 22:44
  • Try to print out the current working directory: var_dump(getcwd()); – zsolt.k Mar 14 '16 at 23:30
  • Thanks for all the comments. The PHP is 5.5. I have tried the absolute path and nothing happens. The var_dump yields the value of the variable. I will try the __DIR__. and get back with the results. – user1794918 Mar 15 '16 at 10:12
  • When I did the var_dump it returned bool(false). But it should return int 3 or whatever the number of digits in the number. – user1794918 Mar 15 '16 at 10:58
  • Gave the absolute path and now the file is written but it is still empty. Could it be the owner of the file is www-data but I see the same permission -rw-r--r-- for the file owner www-data. – user1794918 Mar 15 '16 at 11:57
  • Well, it started working. I added the fopen, fwrite and fclose functions to write the data to the file. The file_put_contents would create the file but not populate it. So, I added the other functions to get what I needed to work. Go figure!! – user1794918 Mar 15 '16 at 15:13

0 Answers0