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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来