如何在上传到文件夹之前更改输入框中提供的图像名称?

huangapple go评论64阅读模式
英文:

How i can change the image name given in input box before uploading it to folder?

问题

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

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

图像名称都将更改为给定的名称。

HTML

<form action="upload.php" method="post" enctype="multipart/form-data">
    <h3>选择要上传的图像:<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="上传图像" name="submit">
</form>
<button class="button" onclick="window.location.href = 'showimgs.php';">显示图像</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 "文件是图像 - " . $check["mime"] . ".";
       $uploadOk = 1;
   } else {
       echo "文件不是图像。";
       $uploadOk = 0;
   }
}

if (file_exists($target_file)) {
   echo "抱歉,文件已存在。";
   $uploadOk = 0;
}

if ($_FILES["fileToUpload"]["size"] > 5000000) {
   echo "抱歉,您的文件太大。";
   $uploadOk = 0;
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
   echo "抱歉,只允许 JPG、JPEG、PNG 和 GIF 文件。";
   $uploadOk = 0;
}

if ($uploadOk == 0) {
   echo "抱歉,您的文件未上传。";

} else {
   if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
       echo "文件" . basename( $_FILES["fileToUpload"]["name"]). "已上传。";
   } else {
       echo "抱歉,上传文件时出错。";
   }
}

echo "<b><button class=button onclick=location.href='images.html'>点击这里返回</button></b>";
?>

我是初学者,这不是我自己的 PHP 代码。

英文:

I just need to change the image name before uploading it to folder.
Whatever user type in here:

&lt;input  name=&quot;yname&quot; placeholder=&quot;Your Name&quot;class=&quot;input-group-field&quot; &gt;

the image name will be changed to given name.

html

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

php

&lt;?php

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

if(isset($_POST[&quot;submit&quot;])) {
   $check = getimagesize($_FILES[&quot;fileToUpload&quot;][&quot;tmp_name&quot;]);
   if($check !== false) {
       echo &quot;File is an image - &quot; . $check[&quot;mime&quot;] . &quot;.&quot;;
       $uploadOk = 1;
   } else {
       echo &quot;File is not an image.&quot;;
       $uploadOk = 0;
   }
}

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

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

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

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

} else {
   if (move_uploaded_file($_FILES[&quot;fileToUpload&quot;][&quot;tmp_name&quot;], $target_file)) {
       echo &quot;The file &quot;. basename( $_FILES[&quot;fileToUpload&quot;][&quot;name&quot;]). &quot; has been uploaded.&quot;;
   } else {
       echo &quot;Sorry, there was an error uploading your file.&quot;;
   }
}

Echo &quot;&lt;b&gt;&lt;button class=button onclick=location.href=&#39;images.html&#39;&gt;Click here to go back&lt;/button&gt;&lt;/b&gt;&quot;;
?&gt;

I am beginner and this is not my own php code.

答案1

得分: 1

以下是您要翻译的内容:

尝试类似以下方式(它还将确保文件名是唯一的):

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '_' . $_POST['yname'] . '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

然后,您还可以使用$newfilename的值将详细信息推送到您的数据库(如果有的话)。

来源:https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory

英文:

Try this something like this (It will also make sure the file name is unique)

$temp = explode(&quot;.&quot;, $_FILES[&quot;file&quot;][&quot;name&quot;]);
$newfilename = round(microtime(true)) . &#39;_&#39; . $_POST[&#39;yname&#39;]. &#39;.&#39; . end($temp);
move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;], &quot;../img/imageDirectory/&quot; . $newfilename);

You can then also use the $newfilename value to push the details to your Database (If you have one)

Source: https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory

huangapple
  • 本文由 发表于 2020年1月6日 22:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定