开发者

upload 2 images at once in codeigniter

开发者 https://www.devze.com 2022-12-31 10:58 出处:网络
can anyone plz provide me a simple working code snippet of codeigniter for uploading 2 images at once (through 2 different input field ofcourse). I 开发者_如何学运维need 2 images to be uploaded at onc

can anyone plz provide me a simple working code snippet of codeigniter for uploading 2 images at once (through 2 different input field ofcourse). I 开发者_如何学运维need 2 images to be uploaded at once, or one after another. and both of the images need to be in different location.

I tried to make it myself by calling upload function twice but it returned last images with these extentions: *.jpg.jpg.

can anyone help


Unfortunately the CodeIgniter upload class doesn't support more than one file. However, you can use the standard PHP functions.

<form enctype="multipart/form-data" action="/upload/send" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Send this file: <input name="a_file" type="file" /><br />
    Send another file: <input name="another_file" type="file" /><br />
    <input type="submit" value="Send Files" />
</form>

Then in the controller do something like this:

class Upload extends Controller {

    function Upload()
    {
        parent::Controller();
    }

    function index()
    {
        $data = array();

        $this->load->view('template/head');
        $this->load->view('upload', $data);
        $this->load->view('template/foot');
    }

    function send()
    {
        // TODO: error checking, and cleanse ['name'] to prevent hacks
        // http://www.php.net/manual/en/features.file-upload.errors.php
        move_uploaded_file(
            $_FILES['a_file']['tmp_name'],
            '/path/to/uploads/'.$_FILES['a_file']['name']
        );
        move_uploaded_file(
            $_FILES['another_file']['tmp_name'],
            '/path/to/uploads/'.$_FILES['another_file']['name']
        );
    }
}

http://www.php.net/manual/en/features.file-upload.post-method.php


Controller

function create(){
        // we are using TinyMCE in this page, so load it
        $this->bep_assets->load_asset_group('TINYMCE');

        if ($this->input->post('name')){
            // fields are filled up so do the followings
            $this->MProducts->insertProduct();
            redirect('products/admin/index','refresh');
        }else{
            // this must be the first time, so set variables
            $data['title'] = "Create Product";

            $this->load->view('image_upload',$data);
        }                                               
    }

Model

function insertProduct(){
        $data = array( 
            'name'          => db_clean($_POST['name']),
            'shortdesc'     => db_clean($_POST['shortdesc']),
            'longdesc'      => db_clean($_POST['longdesc'],5000),
            ...
                    ...
            'image1'        => db_clean($_POST['image1']),
            'image2'            => db_clean($_POST['image2'])
        );
        $this->db->insert('omc_product', $data);    
        $new_product_id = $this->db->insert_id();
     }

View

echo form_open_multipart('products/admin/create')."\n";

echo "<p><label for='parent'>Category</label><br/>\n";
echo form_dropdown('category_id',$categories) ."</p>\n";


echo "<p><label for='pname'>Name</label><br/>";
$data = array('name'=>'name','id'=>'pname','size'=>25);
echo form_input($data) ."</p>\n";

...

echo "<p><label for='image1'>Select Image</label><br/>";
$data = array('name'=>'image1','id'=>'image1','size'=>80);
echo form_textarea($data) ."</p>\n";

echo "<p><label for='image2'>Select another image</label><br/>";
$data = array('name'=>'image2','id'=>'image2','size'=>80);
echo form_textarea($data) ."</p>\n";

...
...
echo form_submit('submit','create product');
echo form_close();
0

精彩评论

暂无评论...
验证码 换一张
取 消