我在Codeigniter中尝试多个文件上传。我使用以下代码
$count = count($_FILES['userfile']['size']);
for($s=1; $s<=$count; $s++) {
$_FILES['userfile']['name']= $_FILES['userfile']['name'][$s];
$_FILES['userfile']['type']= $_FILES['userfile']['type'][$s];
$_FILES['userfile']['tmp_name']=$_FILES['userfile']['tmp_name'][$s];
$_FILES['userfile']['error']= $_FILES['userfile']['error'][$s];
$_FILES['userfile']['size']= $_FILES['userfile']['size'][$s];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
}
private function set_upload_options(){
$config = array();
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
return $config;
}
我只收到数组中的第一个文件..数组中的文件没有上传...这段代码有什么问题...我无法弄清楚...请帮帮我.. 提前致谢
答案 0 :(得分:2)
我在这里看到两个问题。第一:
$_FILES['userfile']['size']
将以字节为单位返回文件的卷...由于它是一个整数,count
函数每次都会返回1
。改为使用数组$_FILES['userfile']['tmp_name']
:
$count = count($_FILES['userfile']['tmp_name']);
然后,你正在覆盖$_FILES
超全局。因此,您必须使用局部变量来保存它:
$count = count($_FILES['userfile']['tmp_name']);
$files = $_FILES;
for($s=1; $s<=$count; $s++) {
$_FILES['userfile']['name']= $files['userfile']['name'][$s];
$_FILES['userfile']['type']= $files['userfile']['type'][$s];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$s];
$_FILES['userfile']['error']= $files['userfile']['error'][$s];
$_FILES['userfile']['size']= $files['userfile']['size'][$s];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
}
答案 1 :(得分:0)
在方法内的控制器中添加此代码。
$ image_path = $ this-&gt; input-&gt; post(&#39; userfile&#39;);
$count = count($_FILES['userfile']['name']); //If file choosen to upload then ex
// print_r($_FILES); die();
$files = $_FILES['userfile']['name'];
$type = $_FILES['userfile']['type'];
$tmp_name = $_FILES['userfile']['tmp_name'];
$error = $_FILES['userfile']['error'];
$size = $_FILES['userfile']['size'];
//print_r($files); die();
// print_r($_FILES['userfile']['name']); die();
for ($i=0; $i <$count ; $i++)
# code...
{
//File Upload Configuration.
$config['upload_path'] = 'new/';
//echo $config['upload_path'].'<br>';
$config['allowed_types'] = 'gif|jpg|png|JPG|PNG|GIF';
$config['max_size'] = '3048';
$config['overwrite'] = false;
$config['remove_spaces'] = true;
$config['encrypt_name'] = TRUE;
$image_path_name = 'userfile';
$_FILES['userfile']['name']=$files[$i];
$_FILES['userfile']['type'] = $type[$i];
$_FILES['userfile']['tmp_name'] = $tmp_name[$i];
$_FILES['userfile']['error'] = $error[$i];
$_FILES['userfile']['size'] = $size[$i];
$this->load->library('upload', $config); //Load library
$this->upload->initialize($config);
if (!$this->upload->do_upload($image_path_name))
{
$this->session->set_flashdata('file','File not uploaded');
redirect('front/prop_sale');
}
else
{
// File uploaded successfully
$image_path[$i] = base_url().'new/'.$this->upload->file_name;
//print_r($image_path);
}
}