PHP图片上传图片命名?(PHP image uploader rename image?)

2019-09-30 03:58发布

我想它上传到我的服务器时要重命名的图像。 有没有普遍这样做的一个简单的方法? 下面是我使用的PHP代码。 我想将它改名为我穿过的隐藏字段从HTML表单变量。

 //variable from hidden field on form which is from mysql database
 $imageName = $_POST['image_rename'];

 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 8000000))
   {
   if ($_FILES["file"]["error"] > 0)
     {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
     }
   else
     {
     echo "Upload: " . $_FILES["file"]["name"] . "<br />";
     echo "Type: " . $_FILES["file"]["type"] . "<br />";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

     if (file_exists("../uploads/" . $_FILES["file"]["name"]))
       {
       echo $_FILES["file"]["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);
       echo "Stored in: " . "../uploads/" . $_FILES["file"]["name"];
       }
     }
   }
 else
   {
   echo "Invalid file";
   }

Answer 1:

尝试改变move_uploaded_file()功能; 添加你想要的功能内的名称。 只要确保抢到合适的扩展,是这样的:

$parts=explode('.',$_FILES['file']['name']);
$newName=$imageName.'.'.$parts[(count($parts)-1)];
move_uploaded_file($_FILES['file']['tmp_name'],'../uploads/'.$newName);


Answer 2:

更改

 move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);

 move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $imageName);


文章来源: PHP image uploader rename image?