我有一个工作多重上传(文件和图像)CODEIGNITER [英] i have a working multiple upload (document and image) CODEIGNITER

查看:62
本文介绍了我有一个工作多重上传(文件和图像)CODEIGNITER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在codeigniter工作多个上传,但我有一个问题,我如何将这些文件的文件名插入两个表(文档和图像表),这两个表有两个相同的列名(ID,名称)。是有办法,我可以分离或包装我的代码上传图像和文档。因为我将它们组合在一个函数中。

I have made a working multiple upload in codeigniter but I'm having a problem on how am I gonna insert the file names of those files on two tables (documents and images table) which these two tables have two the same column name (ID, name). is there way that i could disjunct or compart my code for uploading image and doc. because I united them in one function.

这里是我的CODE。它正在工作。

here is my CODE. it is working.

VIEW

<?php echo form_open_multipart('test'); ?>
   <label>Images</label>
      <input type='file' multiple='multiple' name='userfile[]'>
   <label>Documents</label>
      <input type='file' multiple='multiple' name='userfile[]'>
<?php echo form_submit('submit', 'Upload them files!') ?>

CONTROLLER

CONTROLLER

function index()
{
    if (isset($_POST['submit']))
    {
        $this->load->library('upload');
        //$this->uploadfile($_FILES['userfile']);
        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i<$cpt; $i++)
        {   

                $filename = $_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();
                $this->upload->data();
        }
    }
$this->load->view("test");
}
private function set_upload_options()
{   
//  upload an image and document options
    $config = array();
    $config['upload_path'] = './upload_documents/';
    $config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG|pdf|doc|docx|xls|xlsx';
    $config['max_size'] = '0'; // 0 = no file size limit
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $config['overwrite'] = TRUE;


    return $config;
}

这些代码工作正常,可以将所有文件路径。
但我想知道做MODEL,我怎么识别文件类型,因为你看到我的代码上面我通过变量$文件名传递文件的名称。如果你使用print_r($ filename),你会看到所有的文件名和它的文件扩展名。这些名称是我将根据他们的文件类型插入到我的两个表。

These codes are working and it is able to transfer all the files on my desired path. but I was wondering on making the MODEL, how am I gonna identify the file type as you seen my codes above I passed the name of the file on a variable "$filename". If you use print_r($filename), you'll see all the file name and it's file extension. Those names is the one that I will insert to my two tables accordingly to their type of file.

有任何CodeIgniter或PHP代码,我将用来识别文件类型,并将其传递给模型与两个函数像upload_image或upload_docu?请帮助。

Is there any code for CodeIgniter or PHP code that I will use to identify the file type and pass it to the model with two function like upload_image or upload_docu? Help please.

推荐答案

CONTROLLER

CONTROLLER

function index()
{
    if (isset($_POST['submit']))
    {
        $this->load->library('upload');
        //$this->uploadfile($_FILES['userfile']);
        $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();
                $this->upload->data();
                $ext = pathinfo($filename, PATHINFO_EXTENSION);
                $img_ext_chk = array('jpg','png','gif','jpeg','JPG','PNG', 'GIF', 'JPEG');
                if (in_array($ext,$img_ext_chk))
                {
                    $this->asset->add_image($filename);                            
                }
                else
                {
                    $this->asset->add_document($filename);
                }
        }
    }
}

到您的MODEL

public function add_image($filename)
{
$data = array ('images' => $filename);
$this->db->insert('asset_images', $data);
}

public function add_document($filename)
{
$data = array ('documents' => $filename);
$this->db->insert('asset_documents', $data);
}

这篇关于我有一个工作多重上传(文件和图像)CODEIGNITER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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