【问题标题】:upload 2 images at once in codeigniter在codeigniter中一次上传2张图片
【发布时间】:2010-05-16 06:26:59
【问题描述】:

谁能给我一个简单的codeigniter工作代码sn-p,用于一次上传2张图片(当然,通过2个不同的输入字段)。我需要一次上传 2 张图片,或者一张接一张。并且两个图像都需要位于不同的位置。

我尝试通过两次调用上传函数来自己制作它,但它返回了最后一张带有这些扩展名的图像:*.jpg.jpg。

谁能帮忙

【问题讨论】:

    标签: codeigniter upload


    【解决方案1】:

    不幸的是,CodeIgniter 上传类不支持多个文件。但是,您可以使用标准的 PHP 函数。

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

    然后在控制器中做这样的事情:

    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

    【讨论】:

    • 你确定罗伯特它会工作,因为我遇到了麻烦
    【解决方案2】:

    控制器

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

    型号

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

    查看

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多