Shinu Thomas
Shinu Thomas

Reputation: 5316

Giving file write permission using php

I have a file in my project folder.How i can i give file write permission using php.I used this code

chmod($file,0777);

But it giving an error

Warning: chmod() [function.chmod]: Operation not permitted 

The file is created by another user.Is their any way to do this .Thanks in advance

Upvotes: 1

Views: 1128

Answers (4)

rthrwht
rthrwht

Reputation: 21

you can install php via SUEXEC or SUPHP instead of mod_php which allows to change the user as which php is executed, still this dosnt solve anything if the owner is set wrong

Upvotes: 0

Alin P.
Alin P.

Reputation: 44346

This happens because PHP does not have rights to do the change. The user under which PHP runs is usually the web server's user and different from the user you use to add files.

You generally only do chmod on files created with PHP. To be able to do this on other files you need to change the owner (chown).

Upvotes: 5

Aurel Bílý
Aurel Bílý

Reputation: 7963

Well - you just can't if it says you are not permitted to. Point is - the file belongs to some user and group, most likely root:root - and you are just a user on the server. If root's the owner of that file, you can't change the permissions at all.

Notes:

$file must be a filename. If you put the handle there, the file (most likely) doesn't exists, but still.

Check if the filename is not beginning with / or something. Try different variations.

Upvotes: 3

Shoban
Shoban

Reputation: 23016

The current user is the user under which PHP runs. It is probably not the same user you use for normal shell or FTP access. The mode can be changed only by user who owns the file on most systems.

From http://php.net/manual/en/function.chmod.php

Upvotes: 5

Related Questions