rename() madness......

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
heepzahype
Forum Newbie
Posts: 2
Joined: Fri Jan 16, 2004 6:07 pm
Location: London

rename() madness......

Post by heepzahype »

someone shoot me.... Please!
I am just geting started at this and cannot even get the rename() command to work.

I have a test file called rename.txt and an trying:

<?php
rename("/home/zmqqxh/public_html/rename.txt", "/home/zmqqxh/public_html/testrenamed.txt");
?>

The page returns:

Warning: rename(/home/zmqqxh/public_html/rename.txt,/home/zmqqxh/public_html/renameworked.txt): Permission denied in /home/zmqqxh/public_html/rename.php on line 2

So now I'm confused - what permissions?!?! How do I get this sorted - it's been making me mental for 2 hours now. 8O

Thanks in advance to any kind soul who can help me on this.

?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

you have read only access prolly, try chmod ing the file to 777----should do the trick
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Each file or folder has an owner and group. The permissions set specify read, write and execute access for the owner, groups, and anonymous requests.

Rather than CHMOD 777 (which you can't do in php by the way since it's not the owner - use your ftp program), it might be better to create the file with php to begin with. This means that php is the owner and so it can be left on 755 or something to control access.

With 755, the first 7 specifies owner permissions: = rwx. Everything else gets 5 = r_x ie read and execute.

Note that, to write a file with php, php must have write permissions in that directory. You may need to CHMOD 777 the parent folder (ie if it's not owned by php), write the file with php, then CHMOD the parent back to 755 or whatever.

Once the file is written with php as the owner, parent permissions don't affect subsequent access by php hence you can close the folder down again.
heepzahype
Forum Newbie
Posts: 2
Joined: Fri Jan 16, 2004 6:07 pm
Location: London

Thanks

Post by heepzahype »

Thanks guys. Got it.

So, if I use FTP to upload then PHP will never have permission to perform file opertations (unless the file's permission is changed beforehand) - is this correct?

So, is there some way to upload with PHP then means it can own a whole directory and therefore avoid this whole issue? Or similarly change ownership of a file or directory once it has been created by something else?

Thanks again - amazing how a couple of lines of explination can clear up hours of confusion. :wink:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Thanks

Post by McGruff »

heepzahype wrote:Thanks guys. Got it.

So, if I use FTP to upload then PHP will never have permission to perform file opertations (unless the file's permission is changed beforehand) - is this correct?

So, is there some way to upload with PHP then means it can own a whole directory and therefore avoid this whole issue? Or similarly change ownership of a file or directory once it has been created by something else?

Thanks again - amazing how a couple of lines of explination can clear up hours of confusion. :wink:
Yes - ftp creates files with a different owner to the UID php runs under. 777 opens files up for anything - not that you'd really want to except maybe temporarily.

If you do a dirinfo in your ftp program, you can see what owner/groups are set for files and folders.

AFAIK only the superuser can change a file's owner.
Post Reply