如何将上传的文件保存到目录之前重命名? [英] How to rename uploaded file before saving it into a directory?

查看:162
本文介绍了如何将上传的文件保存到目录之前重命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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



我试过这样做:

  $ allowedExts = array(gif,jpeg,jpg,png); 
$ temp = explode(。,$ _FILES [file] [name]);
$ extension = end($ temp);
if((($ _FILES [file] [type] ==image / gif)
||($ _FILES [file] [type] ==图片/ 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)
{
echoReturn Code: 。 $ _FILES [file] [error]。 <峰; br> 中;

else

$ b $ fileName = $ temp [0]。。。$ temp [1];
$ temp [0] = rand(0,3000); //设置为随机数
$ fileName;
$ b $ if(file_exists(../ img / imageDirectory /。$ _FILES [file] [name]))
{
echo $ _FILES [文件名] 。 已经存在。 ;


$ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],../img/imageDirectory/$ _FILES [file ][名称]);
回声存储在:。 ../img/imageDirectory/。 $ _FILES [ 文件] [ 名称];





echoInvalid file;
}

我试着改变 $ _ FILES [file使用 $ fileName; 变量替换它,以便可以存储新名称。

解决方案

您可以通过更改 move_uploaded_file 的第二个参数中的文件名来简单地更改文件的名称。


$ b

而不是

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

使用

  $ temp = explode(。,$ _FILES [file] [name]); 
$ newfilename = round(microtime(true))。 ''。端($温度);
move_uploaded_file($ _ FILES [file] [tmp_name],../img/imageDirectory/。$ newfilename);

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

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.

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.

解决方案

You can simply change the name of the file by changing the name of the file in the second parameter of 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.

这篇关于如何将上传的文件保存到目录之前重命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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