重命名目录php

时间:2015-11-06 07:22:34

标签: php sql

我想重命名上传到文本display_pic的所有内容 但是,当我上传某些内容时,它会重命名为我在计算机上命名的内容

      $username =getfield('username'); //function to get field name from DB
        $name=$_FILES['file']['name'];
        $size = $_FILES['file']['size'];//for images it says image/jpeg
        /* $max_size = 8000000; //8megabytes =8000000bytes */
    $extension = strtolower (substr($name, strpos($name,'.')+1));  
    $type = $_FILES['file']['type'];
    $tmp_name = $_FILES['file']['tmp_name'];
            if (isset ($name))
            {
              if (!empty($name))
                {
    if ($extension =='jpg' ||$extension =='jpeg' && $type=='image/jpeg')
            {
        $location = 'user/'.$username.'/display_pic.'.$extension;

        if (move_uploaded_file($tmp_name,$location.$name))
            {
            echo 'uploaded';
                }else
                    {
                    echo 'there was an error';
                    }

                    }else 
                        {
                            echo 'file must be a image';
                        }
                }else
                        {
                            echo 'Please choose a file';
                        }   
            }[This is the output i get and the files name m uploading is gear.jpg][1]

1 个答案:

答案 0 :(得分:0)

您正在将文件保存到原始名称本身。 ($location.$name)
更改此部分并将$name设置为您要为扩展名命名的文件。

将您的代码更新为此。

$location = 'user/'.$username.'/display_pic.'.$extension;
move_uploaded_file($tmp_name,$location);