重定向停止多个文件上传CodeIgniter 3.0.4

时间:2016-02-18 08:26:13

标签: codeigniter

当表单成功填满时,我试图在我的上传功能上使用我的filemanager控制器上的重定向。

当我在表单的成功部分有redirect('filemanager')时,它会阻止多个文件上传,然后只上传一个。

  

但是当我像// redirect('filemanager')那样评论它时,我可以   上传多张图片。

问题:我如何仍然在成功表单上使用我的重定向,但请确保上传所选的多个图片而不仅仅是一个。

控制器

<?php

class Filemanager extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
    }

    public function index() {
        $data['title'] = 'File Manager';

        $data['template'] = 'template/common/filemanager_view';
        $this->load->view('template/common/template_view', $data);
    }

    public function upload() {

        $files = $_FILES;

        if ($files) {

            $cpt = count($_FILES['userfile']['name']);

            for($i=0; $i<$cpt; $i++) {           

                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

                $url = '';

                $directory = $this->input->get('directory');

                if (isset($directory)) {
                    $url .= $directory . '/';
                } else {
                    $url .= '';
                }  

                $config['upload_path'] = FCPATH . 'images/catalog/' . $url;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 5000;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['overwrite'] = TRUE;

                $this->upload->initialize($config);

                if (!$this->upload->do_upload()) {

                    // reminder todo some error code here

                } else {

                    redirect('filemanager');

                }
            }

        }

    }
}

1 个答案:

答案 0 :(得分:0)

由于@Vinie建议

,我现在已经开始工作了
<?php

class Filemanager extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
    }

    public function index($results = NULL) {
        $data['title'] = 'File Manager';

        $data['upload_data'] = array();

        if ($results) {
            foreach ($results as $result) {
                $data['upload_data'][] = array(
                    'file_name' => $result['file_name']
                );
            }
        }

        $upload_errors = $this->upload->display_errors();

        if (isset($upload_errors)) {
            $data['upload_errors'] = $upload_errors;
        } else {
            $data['upload_errors'] = '';
        }

        $data['template'] = 'template/common/upload_view';
        $this->load->view('template/common/template_view', $data);
    }

    public function upload() {

        $files = $_FILES;

        if ($files) {

            $check_if_any_error = 0;

            $cpt = count($_FILES['userfile']['name']);

            for($i=0; $i<$cpt; $i++) {           

                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

                $url = '';

                $directory = $this->input->get('directory');

                if (isset($directory)) {
                    $url .= $directory . '/';
                } else {
                    $url .= '';
                }  

                $config['upload_path'] = FCPATH . 'images/catalog/' . $url;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 5000;
                $config['max_width'] = 0;
                $config['max_height'] = 0;
                $config['overwrite'] = TRUE;

                $this->upload->initialize($config);

                if (!$this->upload->do_upload()) {

                    $check_if_any_error++;

                } else {

                    $results[] = $this->upload->data();
                }


            }

            if($check_if_any_error > 0 ) {

                $this->index();

            } else {

                $this->index($results);

            }

        } else {

            redirect('filemanager');

        }
    }
}