1

我正在尝试在将图像上传到我的服务器时重命名它。通常有一种简单的方法可以做到这一点吗?下面是我正在使用的 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";
   }
4

2 回答 2

1

尝试更改move_uploaded_file()功能;在函数中添加您想要的名称。只需确保获取正确的扩展名,例如:

$parts=explode('.',$_FILES['file']['name']);
$newName=$imageName.'.'.$parts[(count($parts)-1)];
move_uploaded_file($_FILES['file']['tmp_name'],'../uploads/'.$newName);
于 2012-04-28T23:09:25.020 回答
0

改变

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

 move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $imageName);
于 2012-04-28T23:08:40.187 回答