0

我有此代码上传 .png 图像。我将在哪里更改代码以将文件名更改为现有的 $_POST['var_name'] 也用于上传表单?

 <?php
 $directory = uploaded_labels; // Name of directory that file is being saved to
 $var_name = $_POST["var_name"]; // Variable name from form to change the name to
 $allowedExts = array("png"); // Allowed extensions
 $temp = explode(".", $_FILES["file"]["name"]);
 $extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/png"))
     && ($_FILES["file"]["size"] < 20000)
     && in_array($extension, $allowedExts))
   {
       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("$directory/" . $_FILES["file"] ["name"]))
       {
       echo $_FILES["file"] ["name"] . " already exists. ";
       }
      else
       {     
   move_uploaded_file($_FILES["file"]["tmp_name"],
      $directory."/" . $_FILES["file"] ["name"]);
      echo "Stored in: " . $directory."/" .$_FILES["file"] ["name"];
     }
    }
   }
else
   {
   echo "Invalid file";
   }
 ?> 
4

2 回答 2

0

你的意思是:

$destination = $directory."/" .$var_name;
if( move_uploaded_file($_FILES["file"]["tmp_name"], $destination) ) {
    echo "Stored in: " . $destination;
}
else {
    echo "file not uploaded";
}
于 2013-08-11T02:01:32.743 回答
0
move_uploaded_file( 
    $_FILES["file"]["tmp_name"], 
    $directory."/" . $_FILES["file"] ["name"]
);

此函数的第二个参数是移动文件的目的地。您可以在此处更改要保存的文件名。

move_uploaded_file( 
    $_FILES["file"]["tmp_name"], 
    $directory."/" . "THE_WHATEVER_NAME_YOU_WANT_TO_USE"  //could be $var_name = $_POST["var_name"];
);
于 2013-08-11T02:01:54.380 回答