我已经在codeigniter中进行了多次上传,但我遇到了如何在两个表(文档和图像表)中插入这些文件的文件名的问题,这两个表有两个相同的列名(身份证,姓名)。有没有办法,我可以分离或隔离我的代码上传图像和文档。因为我把他们联合起来了。
这是我的CODE。它正在发挥作用。
查看
<?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
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;
}
这些代码正常运行,可以传输我所需路径上的所有文件。 但我想知道如何制作模型,我如何识别文件类型,因为你看到我上面的代码我在变量“$ filename”上传递了文件的名称。如果您使用print_r($ filename),您将看到所有文件名及其文件扩展名。这些名称是我将根据其文件类型插入到我的两个表中的名称。
我是否会使用CodeIgniter或PHP代码来识别文件类型并使用upload_image或upload_docu这两个函数将其传递给模型?请帮忙。
答案 0 :(得分:2)
所以这里是..
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);
}
}
}
}
和你的模特
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);
}