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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂