How to upload multiple files in codeigniter 3.0.1. There are similar issues and solutions in stackoverflow but unfortunately non of them are helping to fix the issue I am facing.
This is the error message appearing You did not select a file to upload
with my current code
view (addGallery)
<section>
<h2>Add Gallery</h2>
<?php echo form_open('Newsupload/gallery', ['id'=>'news', 'name'=>'news', 'method'=>'post','enctype'=>'multipart/form-data']) ?>
<div class="grp width-50">
<label for="name">Album Name</label>
<input type="text" name="name" id="name" value="" placeholder="">
</div>
<div class="grp width-100">
<div id="selectedFiles"></div>
<input type="file" id="files" name="files[]" multiple size="20"><br/>
</div>
<?php if (isset($error)) {
echo $error;
} ?>
<grp class="grp width-100">
<button>Add</button>
</grp>
</form>
</section>
controller (gallery)
public function gallery()
{
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($this->set_upload_options());
// $this->upload->do_upload('files[]');
if (!$this->upload->do_upload('files[]'))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
}
}
public function set_upload_options()
{
$config['upload_path'] = getcwd().'/upload/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['remove_spaces'] = true;
return $config;
}
答案 0 :(得分:2)
I think you need to change this line:
if (!$this->upload->do_upload('files[]'))
to
if (!$this->upload->do_upload('files'))
答案 1 :(得分:2)
默认情况下,codeIgniter不支持多文件上传。所以你可以使用 这个图书馆CodeIgniter Multiple Upload Library
答案 2 :(得分:2)
你没有将文件名传递给上传功能。试试这个。
if (!$this->upload->do_upload($_FILES['files']['name']))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
答案 3 :(得分:0)
try this:
$files = $_FILES;
$cpt = count($_FILES['files']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['files']['name'];
$images[] = $fileName;
and make a function set_upload_options()
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
For more Upload multiple files using codeigniter try this http://w3code.in/2015/09/upload-file-using-codeigniter/
答案 4 :(得分:0)
请试试以下
for($i=0; $i<$cpt; $i++)
{
$_FILES['files'] = $files[$i];
$this->upload->initialize($this->set_upload_options());
if (!$this->upload->do_upload('files'))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
}
答案 5 :(得分:0)
您可以在一个请求中使用CodeIgniter上传多个文件。您只需要为每个文件执行do_upload()
。这是一个实现。如果需要,您可以将其设置为循环。
// Image upload Config
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000000';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
// Upload Files
if ( $_FILES['first_file']['name'] && $this->upload->do_upload('first_file') ) {
$first_file = $this->upload->data();
}
if ( $_FILES['second_file']['name'] && $this->upload->do_upload('second_file') ) {
$second_file= $this->upload->data();
}
答案 6 :(得分:0)
我从库$this->upload->do_upload('inputelementname')
理解的不一定是输入元素名称。它应该是文件数组的关键。
所以在这种情况下,这应该有效
if($_FILES){
$filecount=count($_FILES['addmediaelement']['name']);
for($i=0; $i<$filecount; $i++){
$_FILES['mediaelement']['name']=$_FILES['addmediaelement']['name'][$i];
$_FILES['mediaelement']['type']=$_FILES['addmediaelement']['type'][$i];
$_FILES['mediaelement']['tmp_name']=$_FILES['addmediaelement']['tmp_name'][$i];
$_FILES['mediaelement']['error']=$_FILES['addmediaelement']['error'][$i];
$_FILES['mediaelement']['size']=$_FILES['addmediaelement']['size'][$i];
$config['upload_path']=path/to/save/file;
$config['file_name']=filealternatename.extension;
$config['max_size']=MAXSIZE; //max size constant
$config['max_width']=MAXWIDTH; //max width constant
$config['max_height']=CMPMAXHEIGHT; //max height constant
$config['allowed_types']='jpg|png';
if(!is_dir($config['upload_path'])){
mkdir($config['upload_path'], 0775);
}
$this->upload->initialize($config);
$imageuploaderres[]=$this->upload->do_upload('mediaelement');
}
}