views:

411

answers:

4

I have set file permissions to 777 yet I cannot write to the file with PHP.

I can clearly see in my FTP client that the file has 0777 permissions and when I do:

echo (true === is_writable('file.txt')) ? 'yes' : 'no';

I get 'no';

I also tried:

echo (true === chmod('file.txt', 0777)) ? 'yes' : 'no';

With the same result.

The directory listing goes something like this:

public_html
    public          0777
        css         0755
        js          0755
        file.txt    0777

And I'm using .htaccess file to redirect all traffic to the public subfolder. Of course, I have excluded the file from rewriting (it is accessible from the browser I checked):

RewriteRule  ^(file).*  - [L]

Why is that?

A: 

You have to chmod the file right after you create it.

function Doo_Chmod($path, $chmod = null)
{
    if (file_exists($path) === true)
    {
     if (is_null($chmod) === true)
     {
      $chmod = (is_file($path) === true) ? 644 : 755;

      if (in_array(get_current_user(), array('apache', 'httpd', 'nobody', 'system', 'webdaemon', 'www', 'www-data')) === true)
      {
       $chmod += 22;
      }
     }

     return chmod($path, octdec(intval($chmod)));
    }

    return false;
}
Alix Axel
I uploaded the file. I didn't create it in PHP.
Richard Knop
Do this: var_dump(get_current_user()); is the current user the same as the FTP user that uploaded the file? If no, that is your problem, you need to CHMOD it with the FTP client and then you can use the function I posted above (or manual CHMODs for that matter).
Alix Axel
A: 

No offense, but is the path to 'file.txt' correct ? (I mean, are you testing the right file)

Isaac Clarke
Yes. I double checked that.
Richard Knop
+1  A: 

Any chance the folder the file is in is not writable by the webserver?

smack0007
+3  A: 

I guess Apache runs as a different user/group than the user/group owning the file. In which case, the file itself needs to be 0777.

public only needs to be 0777 if you plan on adding files to the folder using PHP. Even if the folder itself is not 0777, if the file is and the folder has at least 5 for the user (read/execute), you should be able to write to the file.

In the end, your file tree should look like this:

public_html
    public
        file.txt  0777

Naturally, you won't be able to change those permissions using PHP, but you can do so from your FTP client.

If it still isn't working, PHP might be running in safe mode or you might be using an extension such as PHP Suhosin. You might get better result changing the owner of the file to the same user/group that is running the script.

To get the user/group id of the executing user, you may use the following:

<?php
echo getmyuid().':'.getmygid(); //ex:. 0:0
?>

Then, you may use chown (in the terminal) to change the owner of the file:

> chown 0:0 file.txt
Andrew Moore
Actually it is a shared hosting environment so I don't have access to the shell. But thanks for very helpful answer anyways.I have decided to modify my script so it uses database to write the data to instead of text files. Now it works.
Richard Knop