[SOLVED] copy(): I can't get the paths right!

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
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

[SOLVED] copy(): I can't get the paths right!

Post by Skittlewidth »

Hi,

I've been trying to move a file from one folder on a web server to another using copy.

If I use chdir() on the folder the image file is in before calling the function I can sucessfully copy the file within the same folder but with a different name:

Code: Select all

chdir("images");
//from the webroot this folder is contribute/static/images/
copy("image.jpg", "newimage.jpg");
I can even move it into the next folder up in the hierachy by doing:

Code: Select all

chdir("images");
copy("image.jpg", "../newimage.jpg");
But then it gets too complicated... I need to move the file to another folder which is two folders down from the webroot: (htdocs) so I tried "assets/images/newimage.jpg" but the folder could not be found because this path is not relative to the "images" folder chdir is set to.

So then I tried "../../../assets/images/newimage.jpg" which is the three levels up out of one folder to the root and back down two levels into the other. I've tried many combinations but I either get "no such file or directory" or "basedir restriction in effect...."

The example in the manual only copies and renames a file into the same directory as the source so I got no help here. Can I chdir() mid function to send the file to the correct destination?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you know the location of the destination relative to the document root you can use $_SERVER['DOCUMENT_ROOT'] quite often ;)
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

of course!

Cheers feyd, a quick chmod of the destination folder on top of that (I'd forgotten about permissions too) and it all worked fine. Thanks for the reminder! 8)
Post Reply