Moving files

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
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Moving files

Post by moiseszaragoza »

Hey every one I have been wandering if anyone knows the function to move a file to another directory

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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

;)
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post by moiseszaragoza »

Thanks

I will check it out
User avatar
moiseszaragoza
Forum Commoner
Posts: 87
Joined: Sun Oct 03, 2004 4:04 pm
Location: Ft lauderdale
Contact:

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Both relative and absolute paths are allowed.. Just make sure your basepath is really what you think it is...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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');
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i'm not mistaken the unix command mkdir -p (or mkdirhier) does this for you too ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply