上传文件重命名

时间:2011-07-18 04:58:04

标签: php image-upload

我有这个非常基本的文件上传脚本:

$prefix = "sitename";
$targetPath = "/uploads/sitename/"

$prefix = $prefix . "_";

$filename = $prefix . md5(basename($_FILES['image']['name'])) . md5(time()) . rand(11111,99999); 

if(move_uploaded_file($_FILES['image']['tmp_name '], $targetPath)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

但我需要将图片重命名为$filename,然后再移至$targetPath中设置的位置。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以在move_uploaded_file的第二个参数中指定新文件名。

示例:

move_uploaded_file($_FILES['image']['tmp_name'], $targetPath . $filename);