如何在将上传的文件保存到目录之前重命名?

时间:2022-03-10 06:40:22

Below is the code I used in order to upload files into a directory. It works fine. My main question is:

下面是我用来将文件上载到目录中的代码。它将正常工作。我的主要问题是:

move_uploaded_file() is the one that saves the uploaded file into the directory, and it is also my guess that move_uploaded_file() is the one that sets the name for it.

move_uploaded_file()是将上传的文件保存到目录中的文件,我也猜测是move_uploaded_file()为它设置了名称。

How could I change the name of my file to a random number?

如何将文件的名称更改为随机数?

I have tried to do so below:

我试图在下面这样做:

      $allowedExts = array("gif", "jpeg", "jpg", "png");
      $temp = explode(".", $_FILES["file"]["name"]);
      $extension = end($temp);
      if ((($_FILES["file"]["type"] == "image/gif")
      || ($_FILES["file"]["type"] == "image/jpeg")
      || ($_FILES["file"]["type"] == "image/jpg")
      || ($_FILES["file"]["type"] == "image/pjpeg")
      || ($_FILES["file"]["type"] == "image/x-png")
      || ($_FILES["file"]["type"] == "image/png"))
      && ($_FILES["file"]["size"] < 100000)
      && in_array($extension, $allowedExts))
        {
        if ($_FILES["file"]["error"] > 0)
          {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          }
        else 
          {

            $fileName = $temp[0].".".$temp[1];
            $temp[0] = rand(0, 3000); //Set to random number
            $fileName;

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

I tried changing variables such as the $_FILES["file"]["name"] and replacing it with the $fileName; variable so that the new name can be stored.

我尝试改变变量,例如$_FILES["file"]["name"],并用$fileName代替;变量,以便可以存储新名称。

3 个解决方案

#1


113  

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

只需在move_uploaded_file的第二个参数中更改文件的名称,就可以更改文件的名称。

Instead of

而不是

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

Use

使用

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

Changed to reflect your question, will product a random number based on the current time and append the extension from the originally uploaded file.

更改为反映您的问题,将根据当前时间生成一个随机数,并从最初上传的文件中追加扩展。

#2


2  

You can Try this,

你可以试试这个,

$newfilename= date('dmYHis').str_replace(" ", "", basename($_FILES["file"]["name"]));

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

#3


1  

You guess correctly. Read the manual page for move_uploaded_file. Set the second parameter to whereever your want to save the file.

你猜正确。读取move_uploaded_file的手册页。将第二个参数设置为您想要保存文件的位置。

If it doesn't work, there is something wrong with your $fileName. Please post your most recent code.

如果它不工作,你的$文件名有问题。请张贴您最近的代码。

#1


113  

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

只需在move_uploaded_file的第二个参数中更改文件的名称,就可以更改文件的名称。

Instead of

而不是

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

Use

使用

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

Changed to reflect your question, will product a random number based on the current time and append the extension from the originally uploaded file.

更改为反映您的问题,将根据当前时间生成一个随机数,并从最初上传的文件中追加扩展。

#2


2  

You can Try this,

你可以试试这个,

$newfilename= date('dmYHis').str_replace(" ", "", basename($_FILES["file"]["name"]));

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);

#3


1  

You guess correctly. Read the manual page for move_uploaded_file. Set the second parameter to whereever your want to save the file.

你猜正确。读取move_uploaded_file的手册页。将第二个参数设置为您想要保存文件的位置。

If it doesn't work, there is something wrong with your $fileName. Please post your most recent code.

如果它不工作,你的$文件名有问题。请张贴您最近的代码。