我有单个图片的上传字段,我试图让它在上传时重命名该图片
如果用户上传名为picture.jpg的图片
它会将其重命名为O1CCJDSXBOM2.jpg
$file = $_FILES['photo']['name'];
$file_loc = $_FILES['photo']['tmp_name'];
$file_size = $_FILES['photo']['size'];
$folder = "../uploads/";
答案 0 :(得分:0)
在上传时,您可以像这样重命名
$target_dir = "..uploads/";
$target_file = $target_dir."O1CCJDSXBOM2.jpg";
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
move_uploaded_file
(第一个参数是您上传的文件,第二个参数是您要移动文件名为ex: uploads/O1CCJDSXBOM2.jpg
的文件)
安全说明:
1)检查图像文件是实际图像还是伪图像
2)检查文件是否已存在
3)检查文件大小
4)允许某些文件格式
5)检查文件大小
答案 1 :(得分:0)
假设您要使用原始名称重命名上传的文件,请尝试以下操作:
$tmp_filename = realpath($_FILES['upload']['tmp_name']);
if ($_FILES['upload']['error'] > 0) {
echo "Error: " . $_FILES['upload']['tmp_name'] . "<br />" . $_FILES['upload']['error'][$i];
} else {
$new_filename = realpath("../uploads") . DIRECTORY_SEPARATOR . $_FILES['upload']['name'];
if (move_uploaded_file($tmp_filename, $new_filename)) {
echo "file moved to " . $new_filename;
} else {
echo "error moving uploaded file.";
}
}
有关详细信息,请参阅move_uploaded_file,Handling file uploads。