如何在CodeIgniter中通过验证上传多个图像

时间:2016-05-18 04:19:13

标签: php codeigniter codeigniter-2 codeigniter-3

我正在尝试通过验证上传多张图片但无法上传。

我的观看代码如下: -

<?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();?>

期待解决方案。

3 个答案:

答案 0 :(得分:2)

按照自己的方式行事,表格应如下:

    <?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();?>

你的控制器应该是:

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
        }
    }
}

答案 1 :(得分:0)

像这样创建一个输入字段数组

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

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


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

</form>

和你的控制者

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;
}

答案 2 :(得分:0)

你已在控制器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'];
    }