繁体   English   中英

重命名并上传文件

[英]Rename and upload file

我正在使用此代码(来自w3schools的代码),我想在上传之前为每个图像名称添加随机数,例如: <?php $randomnum = rand(470000,900000); ?> <?php $randomnum = rand(470000,900000); ?>我怎么可以这样做?

if($_FILES["image"]){
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
$error =  "Sorry, file already exists. Rename the file and try again.";
$uploadOk = 0;
}
if ($_FILES["image"]["size"] > 500000) {
$error =  "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$error =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
    $image = basename($_FILES["image"]["name"]);
} else {
    $error = "Sorry, there was an error uploading your file.";
}
}
}

我在项目中使用了这个。

$name = round(microtime(true)) . substr(md5(rand()), 0, 4) . '.' . end($temp);
$newfilename = 'files/uploads/' . $name;
move_uploaded_file($_FILES["image"]["tmp_name"], $newfilename);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM