我正在尝试弄清楚如何通过下面的代码重命名上传到我的服务器的照片。我可以正确上传照片,但我不知道如何更改名称。
例如,我如何重命名照片,以便将其命名为"renamed_image"?
谢谢!
<?php
$target_path = "./user_photos/";
$target_path = $target_path . basename( $_FILES['picture']['name']);
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['picture']['name'])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>发布于 2013-12-20 15:29:13
$filename = basename($_FILES['file']['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new = md5($filename).'.'.$extension;
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$new}"))
{
// other code
}希望这能有所帮助
快乐编码
发布于 2013-12-20 15:30:34
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.doc','.docx','.rtf','.pdf');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
{
// Rename file
$newfilename = md5($file_basename) . $file_ext;
if (file_exists("upload/" . $newfilename))
{
// file already exists error
echo "You have already uploaded this file.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
echo "File uploaded successfully.";
}
}发布于 2013-12-20 15:31:07
只需使用:
$target_path = $target_path . $username . $extension;
move_uploaded_file($_FILES['picture']['tmp_name'], $target_path);https://stackoverflow.com/questions/20698392
复制相似问题