在上传到文件夹之前如何更改图像名称? [英] How i can change the image name before uploading it to folder?

查看:120
本文介绍了在上传到文件夹之前如何更改图像名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙吗,我只需要更改图像名称,然后再将其上传到文件夹即可. 此处输入的任何用户:

can someone please help i just need to change the image name before uploading it to folder. whatever user type in here:

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

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

the file name will be changed to given name. 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代码. 编辑. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\

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

推荐答案

我不知道您要执行的操作或执行操作的原因,因为您没有提供要更改为的完整详细信息还是为什么,但这是代码:

I don't know what you are trying to do or why you are doing it, as you haven't provided full details of what you want to change to or why, but here is the code :

<?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>";
?>

这篇关于在上传到文件夹之前如何更改图像名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆