我有一个上传代码的文件。一切都很好,但我想在加载文件时重命名该文件。我的代码在这里。我只需要重命名功能和它应该放在哪里。这段代码非常长,因为将大图像裁剪得很小。
示例:如果文件为10MB,则为40或50 KB
error_reporting(0);
$change = "";
$abc = "";
define("MAX_SIZE", "400");
function getExtension($str) {
$i = strrpos($str, ".");
if (!$i) {
return "";
} $l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}
$errors = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change = '<div class="msgdiv">Unknown Image extension</div>';
$errors = 1;
} else {
$size = filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE * 1024) {
$change = '<div class="msgdiv">You have exceeded the size limit!</div>';
$errors = 1;
} if ($extension == "jpg" || $extension == "jpeg") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
} else if ($extension == "png") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
} echo $scr;
list($width, $height) = getimagesize($uploadedfile);
$newwidth = 280;
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$filename = "../adimages/" . $_FILES['file']['name'];
imagejpeg($tmp, $filename, 100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
答案 0 :(得分:0)
只需使用您想要的名称而不是$_FILES['file']['name']
例如:
$filename = "../adimages/". time();
这将使用当前时间戳为每个上传的文件命名。
答案 1 :(得分:0)
更改您的以下内容
$filename = "../adimages/" . $_FILES['file']['name'];
以下内容: -
$new_name = "new_file_name".time();
$filename = "../adimages/" . $new_name;
为唯一性添加了time()。在 new_file_name 的地方,您还可以输入 $ _ FILES ['file'] ['name'] ;
答案 2 :(得分:0)
试试这个 - 只需更换你的行
$filename = "../adimages/" . $_FILES['file']['name'];
使用这个新行
$filename = "../adimages/" .time().'.'.$extension ;