Page 1 of 1

Moving files

Posted: Tue Dec 27, 2005 9:23 pm
by moiseszaragoza
Hey every one I have been wandering if anyone knows the function to move a file to another directory

Thanks

Posted: Tue Dec 27, 2005 9:52 pm
by Chris Corbyn
Just like you use `mv' in UNIX to "move" files (aka, rename files) you use the function rename()

http://www.php.net/rename

;)

Posted: Tue Dec 27, 2005 9:56 pm
by moiseszaragoza
Thanks

I will check it out

Posted: Wed Dec 28, 2005 10:20 am
by moiseszaragoza
by any chance can a directory name contain the ../ or do it has to be a absolute path

error im getting when using rename()

Code: Select all

Warning: rename(../../../imagesSmall/DSCN1148.jpg,/imageDone/DSCN1148.jpg): No such file or directory in /home/content/m/o/i/moiseszaragoza/html/sites/Stockimages/backend/protectedArea/AddImage/moveImage.php on line 92
code

Code: Select all

$dir = "../../../imagesSmall/";
$ImageName=$_GET['ImageName'];
echo("<img src='$dir$ImageName'>"); // this works i do see a image 
rename("$dir$ImageName", "/imageDone/$ImageName");// this is line 92

Posted: Wed Dec 28, 2005 10:35 am
by timvw
Both relative and absolute paths are allowed.. Just make sure your basepath is really what you think it is...

Posted: Wed Dec 28, 2005 11:54 am
by josh
well there's two rules when renaming files


the source file must exist and be readable by apache
the destination *"folder"* must exist

if you have /root/file

and you want to rename it to

/root/folder/folder2/file

you must first mkdir('/root/folder/') and mkdir('/root/folder/2'), BEFORE renaming the original



or use this function I found in the comments on the manual (untested)

Code: Select all

function mkdir_recursive($dirName){
 foreach(split('/',dirname($dirName)) as $dirPart)mkdir($newDir="$newDir$dirPart/");
}
mkdir('/root/folder/folder2/')
rename('/root/file', '/root/folder/folder2/file');

Posted: Wed Dec 28, 2005 1:38 pm
by timvw
If i'm not mistaken the unix command mkdir -p (or mkdirhier) does this for you too ;)

Posted: Wed Dec 28, 2005 6:12 pm
by Chris Corbyn
timvw wrote:If i'm not mistaken the unix command mkdir -p (or mkdirhier) does this for you too ;)
Your not mistaken...

The -p flag indicates that UNIX/Linux should attempt to create the path for you if it doesn't exist.

As for rename() I'm alomst certain the path must exist.... we're programmers... not desktop users :D