首页 > 解决方案 > 在将图像上传到文件夹之前如何更改图像名称?

问题描述

有人可以帮助我只需要在将图像上传到文件夹之前更改图像名称。无论用户在这里输入什么:

<input  name="yname" placeholder="Your Name"class="input-group-field" >

文件名将更改为给定名称。html


<form action="upload.php" method="post" enctype="multipart/form-data">    
    <h3>Select image to upload:<br></br></h3>
    <input  name="yname" placeholder="Your Name"class="input-group-field" ><br>
    <input type="file" class="button" id="fileToUpload" name="fileToUpload">
    <input type="submit" class="button" value="Upload Images" name="submit">
</form>
<button class ="button" onclick="window.location.href = 'showimgs.php';">show images</button>

php



<?php

$target_dir = "photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

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;
   }
}

if (file_exists($target_file)) {
   echo "Sorry, file already exists.";
   $uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 5000000) {
   echo "Sorry, your file is too large.";
   $uploadOk = 0;
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
   echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
   $uploadOk = 0;
}

if ($uploadOk == 0) {
   echo "Sorry, your file was not uploaded.";

} 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.";
   }
}

Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>

我是初学者,这不是我自己的 php 代码。编辑。\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\

标签: phphtml

解决方案


我不知道您要做什么或为什么要这样做,因为您没有提供有关要更改的内容或原因的完整详细信息,但这里是代码:

<?php

$filename = "YOUR_FILE_NAME";

$target_dir = "photos/";
$target_file = $target_dir . $filename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

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;
   }
}

if (file_exists($target_file)) {
   echo "Sorry, file already exists.";
   $uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 5000000) {
   echo "Sorry, your file is too large.";
   $uploadOk = 0;
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
   echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
   $uploadOk = 0;
}

if ($uploadOk == 0) {
   echo "Sorry, your file was not uploaded.";

} 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.";
   }
}

Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>

推荐阅读