How to upload multiple image with validation in CodeIgniter

北战南征 提交于 2019-12-09 13:56:06

问题


I'm trying to upload multiple images with validation but not able to do so.

My view code is as below:-

<?php echo form_open("controller/action");?>
    <ul>
        <li>Category Image <input type="file" name="category_img"></li>
        <li>Product Image <input type="file" name="product_img"></li>
        <li>Slider Image <input type="file" name="slider_img"></li>
    </ul>
    <input type="submit" name="submit" class="btn" value="Submit" />
<?php echo form_close();?>

Looking forward for a solution.


回答1:


Doing it in your way, your form should be as :

    <?php
        // success or error status in uploading for each file

        if( !empty( $notification ) )
        {
            echo '
            <p>Notifications : </p>
            <p>'.$notification.'</p>';
        }
    ?>

    <!-- multipart form opening -->

    <?php echo form_open_multipart("image/upload");?>   <!-- "controller/action" -->

        <ul>
            <li>Category Image <input type="file" name="category_img"></li>
            <li>Product Image <input type="file" name="product_img"></li>
            <li>Slider Image <input type="file" name="slider_img"</li>
        </ul>

        <input type="submit" name="submit" class="btn" value="Submit" />

    <?php echo form_close();?>

And your controller should be as :

class Image extends CI_Controller {

    private $data;    // data member for storing status of the uploads

    function __construct()
    {
         // some code
    }

    public function index()
    {
        $this->upload();
    }

    public function upload()
    {
        $this->data['notification'] = '';

        if( $this->input->post('submit') )
        {
            // loading helpers

            $this->load->helper(array('form', 'url'));

            //setting the config array, for more options see Codeigniter docs

            $config['upload_path']          = 'uploads/';       // upload path
            $config['allowed_types']        = 'gif|jpg|jpeg|png';   // allowed file types

            // loading upload library with config array
            $this->load->library('upload', $config);

            // uploading the files, lets_upload() is defined below

            $this->lets_upload( 'category_img' );

            $this->lets_upload( 'product_img' );

            $this->lets_upload( 'slider_img' );
        }

        $this->load->view('form', $this->data);    // form view is loaded along with success or error notification in the 'data' member variable

    }

    public function lets_upload( $field_name )    // '$field_name' refers to input field name
    {
        if ( ! $this->upload->do_upload( $field_name ))     // if uploading failed
        {
            $this->data['notification'] .= $this->upload->display_errors();     // stored error in member variable 'data'
        }
        else        // if uploading success
        {
            $upload_data = $this->upload->data();       // stored the file info in 'data'

            $this->data['notification'] .= $upload_data['file_name']." is successfully uploaded.<br>";      // file name is displayed with 'success' message
        }
    }
}



回答2:


Create an array of your input field like this

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" multiple name="userfile[]" size="20" />
<br /><br />


<input type="submit" value="upload" />

</form>

and in your contrller

function do_upload()
{       
    $this->load->library('upload');

    $files = $_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];    

        $this->upload->initialize($this->set_upload_options());
        $this->upload->do_upload();
    }
}

private function set_upload_options()
{   
    //upload an image options
    $config = array();
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

    return $config;
}



回答3:


U had add in this in controller category_img

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = 250;
    $this->load->library('upload', $config);
    $this->upload->do_upload('category_img');
    $upload_data =  $this->upload->data();
    $file_name =   $upload_data['file_name'];
    $this->upload->initialize($config);
    if (!$this->upload->do_upload('category_img')) {
    $error = array('error' => $this->upload->display_errors());
    $error['error'];
    }
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = 250;
    $this->load->library('upload', $config);
    $this->upload->do_upload('product_img');
    $upload_data =  $this->upload->data();
    $file_name =   $upload_data['file_name'];


    $this->upload->initialize($config);
    if (!$this->upload->do_upload('product_img')) {
    $error1 = array('error1' => $this->upload->display_errors());
    $error1['error1'];
    }
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = 250;
    $this->load->library('upload', $config);
    $this->upload->do_upload('slider_img');
    $upload_data =  $this->upload->data();
    $file_name =   $upload_data['file_name'];
    $this->upload->initialize($config);
    if (!$this->upload->do_upload('slider_img')) {
    $error2 = array('error2' => $this->upload->display_errors());
    $error2['error2'];
    }


来源:https://stackoverflow.com/questions/37290237/how-to-upload-multiple-image-with-validation-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!