duanqiang2977 2015-10-31 05:57
浏览 33

codeigniter中的两个多文件上传器

I have been trying to upload multiple photos twice in a single form in codeigniter project,though the photos are uploading correctly but the name of photos are not been returned as per desired. for example if am uploading a photo named 'a.jpg' in first uploader and 'b.jpg' in second uploader,the names i am getting are "a.jpg,b.jpg" for first and "b.jpg,b(1).jpg" for second case. below is the code attached.

controller:-

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Imageupload extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('imageupload_view', array('error' => ' ' ));
    }

    function doupload()
    {
        $name_array = array();
        $count = count($_FILES['userfile']['size']);
    //  echo $count;
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++)
        {
            $_FILES['userfile']['name']=$value['name'][$s];
            $_FILES['userfile']['type']    = $value['type'][$s];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['userfile']['error']       = $value['error'][$s];
            $_FILES['userfile']['size']    = $value['size'][$s];
            $config['upload_path'] = 'application/views/uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000000';
            //$config['max_width']  = '1024';
            //$config['max_height']  = '768';
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $name_array[] = $data['file_name'];
        }

        $names= implode(',', $name_array);
        return $names;
    }

    function doupload_1()
    {
        $name_array = array();
        $count = count($_FILES['file_1']['size']);
        echo $count;
        foreach($_FILES as $key=>$value)
        for($s=0; $s<=$count-1; $s++)
        {
            $_FILES['file_1']['name']=$value['name'][$s];
            $_FILES['file_1']['type']    = $value['type'][$s];
            $_FILES['file_1']['error']       = $value['error'][$s];
            $_FILES['file_1']['tmp_name'] = $value['tmp_name'][$s];
            $_FILES['file_1']['size']    = $value['size'][$s];
            $config['upload_path'] = 'application/views/uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000000';
            //$config['max_width']  = '1024';
            //$config['max_height']  = '768';
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $name_array[] = $data['file_name'];
        }

        $names= implode(',', $name_array);
        return $names;
    }

    public function check()
    {
        $data = $this->doupload();
        $data_1 = $this->doupload_1();
        print_r($data);
        echo "<br>";
        print_r($data_1);
    }

}
?>

view:-

<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('imageupload/check');?>
<input name="userfile[]" id="userfile" type="file" multiple="" />
<input name="file_1[]" id="file" type="file" multiple=""/>
<input type="submit" value="upload" />
<?php echo form_close() ?>
</body>
</html>

thanks in advance!

  • 写回答

3条回答 默认 最新

  • doubi5127 2015-10-31 06:39
    关注

    Finally i got what the problem in your code. it is because your foreach on $_FILES not on $_FILES['userfile'] thats why $_FILES CONTAIN all the files not only userfile. and file_1

    foreach($_FILES['userfile'] as $key=>$value)
    
    foreach($_FILES['file_1'] as $key=>$value)
    

    but still you have to change the below code according to foreach. if you find solution for your answer please givel like my answer and accept it .

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用