Take the 2-minute tour ×

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          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?

share|improve this question
Do you have write permission to the folder? –  Re0sless Aug 11 '09 at 12:55
Could you provide the directory listing of the file? Is it possible you aren't the owner? Also, it may be an FTP server configuration problem. –  ryanday Aug 11 '09 at 12:56
Try chown nobody file.txt –  karim79 Aug 11 '09 at 13:06
I have edited my post –  Richard Knop Aug 11 '09 at 13:07
Why are you doing the === operator? –  Daniel A. White Aug 11 '09 at 13:17

5 Answers 5

up vote 4 down vote accepted

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:

        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:

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
share|improve this answer
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 Aug 11 '09 at 13:44

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

share|improve this answer

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;
share|improve this answer
I uploaded the file. I didn't create it in PHP. –  Richard Knop Aug 11 '09 at 12:59
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 Aug 11 '09 at 13:07

In opencart i faced this error after installing vqmod and giving all necessary permissions.

after researching a bit, found it.

"MODS CACHE PATH NOT WRITEABLE" is actually refering to vqmod folder itself and not the cache folder.

sudo chmod -R 777 vqmod 

in your root directory.....

share|improve this answer

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

share|improve this answer
Yes. I double checked that. –  Richard Knop Aug 11 '09 at 12:56

Your Answer


