file_put_contents php permissions

13,923

Solution 1

You have to set permissions as 777 for the apache user. Apache also needs to be able to 'create' new files. I'm sure that if you tried writing to a file with 777 permissions that it would work.

Solution 2

The function returns the number of bytes that were written to the file, or FALSE on failure.

Make sure that mydir is writable (by the apache user - apache2 or www-data). To check if content was written to file use the === operator:

$file = "mydir/testfile.txt";

$result = file_put_contents($file,"this is my test");

if ($result === false)
{
  echo "failed to write to $file";
}
else
{
  echo "successfully written $result bytes to $file";
}

For debugging i recommend to define this at the top of your php file:

error_reporting(-1);
ini_set('display_errors', true);
Share:
13,923
hackartist
Author by

hackartist

I am a programmer working out of San Francisco in a mobile and web start-up company. I have worked in C, Java, Labview, Octave, Assembly, Perl, Python, Javascript/jQuery, MySQL, and mostly PHP. I attended Princeton University and the projects I work in normally focus in AI, Machine Learning, Natural Language Processing, or Data Mining.

Updated on June 04, 2022

Comments

  • hackartist
    hackartist almost 2 years

    This is a question about my system configuration and how apache/php can be affected by that. Code that was working on my dev server failed to work when I moved to production and here is the problem I boiled the error down to. I want to use file_put_contents in php to simply make a file on my system. Here is the very simple code I am using:

    print file_put_contents("testfile.txt","this is my test")?"made file":"failed";
    

    No errors are thrown but it always prints "failed". When I run it in the normal directory no file is made but when I move it to a directory with 777 permission, it will make the file but still print "failed" and save nothing into it. If I change the php file itself to have 777 permission nothing changes. If I change the target file (testtext.txt) to have 777 permission nothing changes.

    The when I change the owner of the php file to be the same as the created testfile.txt nothing changes. When I try relative vs. absolute paths nothing changes. For some reason I can have php make the file but it is always empty and using file_put_contents I can't write anything in.

    The created file is in the owner and group of 'www-data' while I am acting as root if that helps. What can I do to get this working? I think that my version of php is using the suhosin extension if this could have anything to do with it (or if you think there is a way to fix this in php or suhosin ini files I can do that too).

    I've been hunting this for hours the last few days.

    EDIT: updates based on the comments below. When I run it in CLI mode it does work but since I need to run it through apache how can I use this fact? Does this tell us something about where or what is stopping the file writing? Also the file_put_contents call is actually returning "false" and not zero.

    • jolivier
      jolivier almost 12 years
      has the www-data user +w and +x rights on the folder which will contain this file? can you su as www-data and try to write to this file?
    • Mitya
      Mitya almost 12 years
      file_put_contents() returns the number of bytes written on successful write. If it's creating an empty file, this is presumably 0 bytes, which would evaluate to a falsy. That's why you're seeing the "failed" message. Try the fopen() / fwrite() route instead. I wouldn't anticipate any change but good to check.
    • Tucker
      Tucker almost 12 years
      Does the script work when you run it CLI?
    • hackartist
      hackartist almost 12 years
      Yes I just ran it in CLI and it did work -- what does this mean for how the permissions are set, since I have to get the apache version to work? It is actually returning false and not 0 when I modified to check the type it was returning by doing print file_put_contents("testfile.txt","this is my test")===false?"failed":"made file";
    • Stano
      Stano almost 12 years
      echo (file_exists('testfile.txt')?'y':'n').getcwd();
  • hackartist
    hackartist almost 12 years
    Yes it seems since CLI works but apache does not, that this is some issue with permissions. In Ubuntu Unix, how does one give permissions to a user -- not a specific file or folder which I am used to doing with chmod?
  • Adam F
    Adam F almost 12 years
    Not Sure. I'm using linux mint and in my natalus it gives me the option to modify the permissions for all the machines users, with WWW-Data being right there.
  • hackartist
    hackartist almost 12 years
    Well even though I have not yet been able to get it to work I am now about 95% sure that it is a permissions issue with apache not having the right permissions to write these files so I will accept this answer.
  • arkascha
    arkascha over 7 years
    A permission of 0777 is nearly never required and rarely a good idea. Instead one should typically use the group memberships of those accounts who need access to the files or even create a new, custom crafted user group for special purposes and grant ownership and access permissions to that group.