如何在CodeIgniter中通过验证上传多个图像 [英] How to upload multiple image with validation in CodeIgniter

查看:69
本文介绍了如何在CodeIgniter中通过验证上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的查看代码如下:-

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

期待解决方案。

推荐答案

以您的方式进行操作,您的表单应为:

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

您的控制器应为:

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

这篇关于如何在CodeIgniter中通过验证上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆