在上传之前完全重命名照片 - php

时间:2014-03-23 20:45:35

标签: php image-upload

我想在上传之前完全重命名文件。我在这里发现的其他帖子似乎在文件名的开头或末尾添加了一些内容以使其独一无二,但我只是想彻底擦除名称并重命名。

输入:anyoldfilename.jpg 输出:thisnameexactly.jpg

以下功能我正在使用上传但不重命名文件。名称应该是$newfilename,我在函数中创建了它:

$target = "../files/photos/"
$target = $target . basename( $_FILES['photo']['name']);

//Find file extension of photo
$newfilename = "thisnameexactly";
$ext = end(explode('.', $_FILES['photo']['name']));
$newfilename = $newfilename . "." . $ext;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
   echo "image successfully uploaded";
}
else {
   echo "Sorry, there was a problem uploading your file.";
}

如何修改它以便完全重命名文件?

1 个答案:

答案 0 :(得分:1)

$target = $target . basename( $_FILES['photo']['name']); 

你有顶部,将其移动为

$target = "../files/photos/"

//Find file extension of photo
$newfilename = "thisnameexactly";
$ext = end(explode('.', $_FILES['photo']['name']));
$newfilename = $newfilename . "." . $ext;

$target = $target . $newfilename;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)){
   echo "image successfully uploaded";
}
else {
   echo "Sorry, there was a problem uploading your file.";
}