dongzhenju3015 2017-03-23 07:40
浏览 41

CodeIgniter文件上传更改循环内的文件名

The case is I have 3 file inputs. Each name attribute for them are file1, file2, and file3

Then inside my controller, I use switch case function for I want to rename the file.

file1 = 'label' file2 = 'meter' file3 = 'rumah'

for ($n=1; $n<=3; $n++) {
    if (empty($_FILES["file$n"]['name'])) continue;

    $filext = pathinfo($_FILES["file$n"]['name'],PATHINFO_EXTENSION);

    switch($n) {
        case 1: $namae = 'label'; break;
        case 2: $namae = 'meter'; break;
        case 3: $namae = 'rumah'; break;
    }

    $config['upload_path']      = $upload_path;
    $config['allowed_types']    = 'gif|jpg|png';
    $config['max_size']         = 1024 * 20;
    $config['file_name']        = $namae;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload("file$n")) {
        ${"photo$n"."_upload"} = $this->upload->display_errors();
    } else {
        ${"photo$n"."_upload"} = $this->upload->data();
        $this->model->simpan_foto($id_survei,$namae,${"photo$n"."_upload"}['file_name']);
    }
}

But it didn't work! All 3 of the files are named with 'label', so it's label.png, label1.png, label2.png (because I not use $config['overwrite'])

How to accomplish this?

  • 写回答

1条回答 默认 最新

  • drlhsfqoa350437979 2017-03-23 10:35
    关注

    if there is only three file than try this code....

    for ($n=1; $n<=3; $n++) {
        if (empty($_FILES["file$n"]['name'])) continue;
    
        $filext = pathinfo($_FILES["file$n"]['name'],PATHINFO_EXTENSION);
    
        if($n==1){
         $namae = 'label';
         }
         if($n==2){
         $namae = 'meter';
         }
         if($n==3){
         $namae = 'rumah';
        }
    
        $config['upload_path']      = $upload_path;
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = 1024 * 20;
        $config['file_name']        = $namae;
    
        $this->load->library('upload', $config);
    
        if (!$this->upload->do_upload("file$n")) {
            ${"photo$n"."_upload"} = $this->upload->display_errors();
        } else {
            ${"photo$n"."_upload"} = $this->upload->data();
            $this->model->simpan_foto($id_survei,$namae,${"photo$n"."_upload"}['file_name']);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度