這兩個(gè)Codeigniter實(shí)現(xiàn)多文件上傳方法都是老外寫的,一個(gè)是用最笨的方式寫的,一個(gè)是用循環(huán)的array方式寫的,兩個(gè)結(jié)合起來(lái)使用,能夠?qū)崿F(xiàn)多文件上傳的目的。
方法一:
view: upload_form.php
<?php echo form_open_multipart('upload'); ?>
<p>
<?php echo form_label('File 1', 'userfile') ?>
<?php echo form_upload('userfile') ?>
</p>
<p>
<?php echo form_label('File 2', 'userfile1') ?>
<?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
controller: upload.php
function index()
{
// Has the form been posted?
if (isset($_POST['submit']))
{
// Load the library - no config specified here
$this->load->library('upload');
// Check if there was a file uploaded - there are other ways to
// check this such as checking the 'error' for the file - if error
// is 0, you are good to code
if (!empty($_FILES['userfile']['name']))
{
// Specify configuration for File 1
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize config for File 1
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
// Do we have a second file?
if (!empty($_FILES['userfile1']['name']))
{
// Config for File 2 - can be completely different to file 1's config
// or if you want to stick with config for file 1, do nothing!
$config['upload_path'] = 'uploads/dir2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize the new config
$this->upload->initialize($config);
// Upload the second file
if ($this->upload->do_upload('userfile1'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
}
else
{
$this->load->view("upload_form");
}
}
原文地址:http://darrenonthe.net/2011/05/08/upload-multiple-files-with-codeigniter/
方法二:
view
<form...
<input type="file" name="files[]">
<input type="file" name="files[]">
...
</form>
controller
load->helper('upload');
multifile_array();
foreach ($_FILES as $file => $file_data)
$this->upload->do_upload($file);
multifile_array文件代碼:
<?php
/**
* Make multifile array input complaint with CI_Upload.<br>
* For use files[ ] input name you must use it method.
*
* @author porquero
*
* @example
* In Controller<br>
* $this->load->helper('upload');<br>
* multifile_array();<br>
* foreach ($_FILES as $file => $file_data) {<br>
* $this->upload->do_upload($file);
* ...
*
* @link http://porquero.blogspot.com/2012/05/codeigniter-multifilearray-upload.html
*/
function multifile_array()
{
if(count($_FILES) == 0)
return;
$files = array();
$all_files = $_FILES['f_file']['name'];
$i = 0;
foreach ($all_files as $filename) {
$files[++$i]['name'] = $filename;
$files[$i]['type'] = current($_FILES['f_file']['type']);
next($_FILES['f_file']['type']);
$files[$i]['tmp_name'] = current($_FILES['f_file']['tmp_name']);
next($_FILES['f_file']['tmp_name']);
$files[$i]['error'] = current($_FILES['f_file']['error']);
next($_FILES['f_file']['error']);
$files[$i]['size'] = current($_FILES['f_file']['size']);
next($_FILES['f_file']['size']);
}
$_FILES = $files;
}