dongsilu2237 2013-05-29 16:43
浏览 30
已采纳

上传时重命名多个文件

I have a form that uploads multiple file. The php code that I am using works fine but I would like to rename the files also and don't know how to go about this. I think adding a time stamp to the name would be the best answer. Here is the working code so far:

/* FILE UPLOAD CODE */
{
    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '/uploads/'; //set upload directory
    /**
     * we get a $_FILES['file'] array ,
     * we procee this array while iterating with simple for loop
     * you can check this array by print_r($_FILES['file']);
     */
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['file']['name'][$i];
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $_FILES['file']['name'][$i])) {
                $number_of_moved_files++;
            }

        }

    }

}
/* END FILE UPLOAD CODE */

Any help on what to add and where to accomplish this would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • douqiaolong0528 2013-05-29 16:54
    关注

    If you want to give the same name to all the uploaded files, then you can get the name by using $_GET. For example:

    <form action="upload.php" method="post"> \\ this would send it to your page
    <input type='text' size='30' name='filename'/>
    <input type='submit' /> 
    </form>
    

    simply get the name using $_GET like this

    $name = $_GET['filename'];
    

    and now run your code but move the file with this name like this

    move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory .         $name[$i])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?