Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

php - How to upload multiple files in codeigniter 3.0.1

How to upload multiple files in codeigniter 3.0.1. There are similar issues and solutions in stackoverflow but unfortunately non of them are helping to fix the issue I am facing.

This is the error message appearing You did not select a file to upload with my current code

view (addGallery)

<section>
    <h2>Add Gallery</h2>
        <?php echo form_open('Newsupload/gallery', ['id'=>'news', 'name'=>'news', 'method'=>'post','enctype'=>'multipart/form-data']) ?>

        <div class="grp width-50">
            <label for="name">Album Name</label>
            <input type="text" name="name" id="name" value="" placeholder="">
        </div>
        <div class="grp width-100">
            <div id="selectedFiles"></div>
            <input type="file" id="files" name="files[]" multiple size="20"><br/>
        </div>
        <?php if (isset($error)) {
            echo $error;
        } ?>
        <grp class="grp width-100">
            <button>Add</button>
        </grp>
    </form>
</section>

controller (gallery)

public function gallery()
{

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

    $files = $_FILES;
    $cpt = count($_FILES['files']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['files']['name']= $files['files']['name'][$i];
        $_FILES['files']['type']= $files['files']['type'][$i];
        $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
        $_FILES['files']['error']= $files['files']['error'][$i];
        $_FILES['files']['size']= $files['files']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        // $this->upload->do_upload('files[]');
        if (!$this->upload->do_upload('files[]'))
        {  
            $error =['error' => $this->upload->display_errors()];
            $this->load->view('admin/addGallery', $error);
        }
    }
}
public function set_upload_options()
{
    $config['upload_path'] = getcwd().'/upload/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['remove_spaces'] = true;
    return $config;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default codeIgniter doesn't support multi-file upload. So you can use this library CodeIgniter Multiple Upload Library


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...