多文件上传(阵列),具有codeIgniter 2.0 [英] Multiple files upload (Array) with CodeIgniter 2.0
问题描述
我一直在寻找和奋斗3天了,使这个工作,但我不能。
我想要做的是使用一个多文件输入表格并上传。我不能只用文件的一个固定数量的上传。我试过在计算器上很多很多的解决方案,但我没能找到工作的。
I've been searching and struggling for 3 days now to make this works but I just can't. What I want to do is use a Multiple file input form and then upload them. I can't just use a fixed number of file to upload. I tried many many solutions on StackOverflow but I wasn't able to find a working one.
下面是我上传控制器
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','html'));
}
function index()
{
$this->load->view('pages/uploadform', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './Images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload');
foreach($_FILES['userfile'] as $key => $value)
{
if( ! empty($key['name']))
{
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($key))
{
$error['error'] = $this->upload->display_errors();
$this->load->view('pages/uploadform', $error);
}
else
{
$data[$key] = array('upload_data' => $this->upload->data());
$this->load->view('pages/uploadsuccess', $data[$key]);
}
}
}
}
}
?>
我的上传表单是这样的。
My upload form is This.
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" multiple name="userfile[]" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
我刚有这个错误:
I just keep having this error :
您没有选择要上传的文件。
You did not select a file to upload.
下面的实施例的阵列
阵列([userfile的] =>阵列([名称] =>阵列([0] => youtube.png [1] => zergling.jpg)[类型] =>阵列([0] =>图像/ PNG [1] =>图像/ JPEG)[tmp_name的值] =>阵列([0] => E:\\ WAMP \\ tmp目录\\ php7AC2.tmp [1] => E:\\ WAMP \\ tmp目录\\ php7AC3.tmp)错误] =>阵列([0] => 0 [1] => 0)[大小] =>阵列([0] => 35266 [1] => 186448)))
Array ( [userfile] => Array ( [name] => Array ( [0] => youtube.png [1] => zergling.jpg ) [type] => Array ( [0] => image/png [1] => image/jpeg ) [tmp_name] => Array ( [0] => E:\wamp\tmp\php7AC2.tmp [1] => E:\wamp\tmp\php7AC3.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 35266 [1] => 186448 ) ) )
我有这就像在一个连续5次,如果我选择2个文件。
我还使用标准库上传
I have this like 5 times in a row if I select 2 files. I also use the standard Upload library.
推荐答案
我终于设法让它与你的工作有所帮助!
I finally managed to make it work with your help!
下面是我的code:
function do_upload()
{
$this->load->library('upload');
$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();
}
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './Images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
感谢你们!
这篇关于多文件上传(阵列),具有codeIgniter 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!