php and moving images to new folders
I have an image in my templates
folder that is made using php with a background template .jpg
(stored in the s开发者_如何学Pythoname folder) and some text submitted from a form. It is saved as a jpg in this folder. I want to copy the php-constructed image to a folder uploads
, and then rename it as image451.jpg
, or whatever the id number is. I think I should use the copy()
and rename()
php functions, but I can't get them to work. How do I do this?
You will need to be sure that your web server has write access to the uploads
directory where you want them to reside. Then you can just do:
if (!rename("oldfilename.jpg", "/path/to/uploads/image451.jpg"))
{
echo "oops, something went wrong moving your file.";
}
We don't know what server platform you're running so I can't be specific on how to check directory write permissions unless you add that info to your question.
You should test if your destination is writeable before trying to move the new file there.
$dest_dir = "/path/to/uploads/";
$src_file = "path/to/orig/file.jpg";
if (is_writeable($dest_dir)) {
$dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($src);
if (false == rename($src, $dest_file)) {
trigger_error('Could not move file', E_USER_ERROR);
}
}
else {
trigger_error('Destination dir is not writeable', E_USER_WARNING);
}
精彩评论