chmod a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

chmod a directory

Post by anjanesh »

Hi

Im trying to chmod a directory to 777 using chmod("folder", 0777); but it gives the error

Code: Select all

Warning: chmod(): Operation not permitted in /xxx/xxx.php on line nn
Actually I want to write data to a file - if the file does not exist then create it. This file may be deleted manually many times. So I cannot chmod it to 777 manually.
I cant use chmod($ilename,0777) because the filename may not exist.

Any idea how to get this done ?
Thanks
nyy2000
Forum Newbie
Posts: 15
Joined: Tue Jul 12, 2005 12:40 am

Re: chmod a directory

Post by nyy2000 »

anjanesh wrote:I cant use chmod($ilename,0777) because the filename may not exist.
How about checking wheter file exists before that using file_exists();?

I guess PHP or Apache running user (www or nobody in most cases) doesn't have permission to write on parent of "folder" directory. You would have to chmod chown the parent directory.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: chmod a directory

Post by anjanesh »

nyy2000 wrote: How about checking wheter file exists before that using file_exists();?
If it doesnt how would I create the file ? For that I would need to do chmod(0777) to that file which doesnt exist !
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

if (!file_exists("/example/file.dat")) {
 fopen('/example/file.dat', 'w');
}
nyy2000
Forum Newbie
Posts: 15
Joined: Tue Jul 12, 2005 12:40 am

Post by nyy2000 »

jshpro2 wrote:

Code: Select all

if (!file_exists("/example/file.dat")) {
 fopen('/example/file.dat', 'w');
}
I think anjanesh is pointing out PHP doesn't have permission to write to that directory.
So, as I said, anjanesh needs to make the parent directory writable to Apache user.
Post Reply