View Full Version : how to rename images and save them to different folder using PHP?

01-29-2007, 08:01 AM
( since this is an entirely different question i am posting it as a new message.)

for example,
i have an image here: htp://domain.com/images1/image.jpg
i would like to rename this image as renamedimage.jpg
and move it to another folder htp://domain.com/images2/renamedimage.jpg

how can this be done using PHP?

( i have thousands of images. so i am thinking of using some php script to do this task )
please reply with possible solutions.
thank you very much

01-29-2007, 09:11 AM
You could use php copy(source, destination) function

01-29-2007, 12:43 PM
hello gunman,
thanks for the tip.
but i have a problem.
i used the code as follows:
i kept it in a while loop because there are thousands of images to rename

$file = 'htp://domain.com/images/image1.jpg';
$newfile = 'htp://domain.com/renamedimages/renamedimage1.jpg';

copy($file, $newfile);

and the result is all the images are the same. ( image1 is copied into image2, image3, image4 and all )
i think i have to use imagedestroy function. but dont know how to use the function in this context.
i have tried several ways and the result is:
imagedestroy(): supplied argument is not a valid Image resource.

i could copy individual files very well using this function.
please inform me how to copy the images recursively.
thank you very much for any help

01-30-2007, 07:51 PM
If the same source file given by the $file variable got copied into each destination $newfile, that would indicate that the $file variable did not get modified within your while loop for each file.

imagedestroy() is an GD image manipulation function and not a file handling function - http://www.php.net/manual/en/ref.filesystem.php If you want to delete a file after it has been successfully copied, use the unlink() function - http://www.php.net/unlink