我希望代码自动重命名文件。因此,如果文件名为image.png,并且另一个用户上传了一个名为image.png的文件,则代码将自动将该文件重命名为image1.png,依此类推。
我当前的代码未上传图片。页面空白,并显示:“文件是图像-image / png。”。
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image. ";
$uploadOk = 0;
}
// Checks if file already exists, and if it does, it should rename it.
while (!file_exists($target_file.".".$imageFileType)) {
$target_file = $target_file.$i++;
$uploadOk = 1;
}
// Checks file size
if ($_FILES["fileToUpload"]["size"] > 500000000) {
echo "Sorry, your file is too large. ";
$uploadOk = 0;
// Only allow specific file types
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed, your uploaded a $imageFileType file. ";
$uploadOk = 0;
}
// Checks if $uploadOk is set to 0 by mistake.
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded. ";
// All is ok
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
我期望代码使用$ target_file并添加$ i ++。我期望它会一直增加数字,直到服务器上没有与它同名的文件为止。