【问题标题】:How to upload multiple image with validation in CodeIgniter如何在 CodeIgniter 中上传多个带有验证的图像
【发布时间】:2016-05-18 04:19:12
【问题描述】:

我正在尝试上传多张经过验证的图片,但无法这样做。

我的查看代码如下:-

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

期待解决方案。

【问题讨论】:

  • 使用 代替 并且还使用输入字段作为数组。

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

按照你的方式做,你的表格应该是:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我有 3 个不同的字段用于上传图片。我还想为此应用验证(必需且有效的图像)。
    • 你可以这样定义它们 对于有效图像,您可以看到我们正在使用 set_upload_options 函数,该函数将检查允许的类型,对于空白图像,您可以检查数组是否包含空白值。
    【解决方案3】:

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

    【讨论】:

    • 我们可以让它通用吗?
    • 请了解在 SO 上发布时如何正确格式化代码。反引号用于“内联”代码,每行前面的 4 个空格用于代码块。适当缩进代码以提高可读性也是一个好主意。
    猜你喜欢
    • 2016-04-13
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2018-11-24
    相关资源
    最近更新 更多