如何在 codeigniter 3.0.1 中上传多个文件
How to upload multiple files in codeigniter 3.0.1
如何在 codeigniter 3.0.1 中上传多个文件。 Whosebug 中有类似的问题和解决方案,但不幸的是,没有一个能帮助解决我面临的问题。
这是 You did not select a file to upload
我当前代码
出现的错误消息
查看(添加画廊)
<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>
控制器(图库)
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;
}
我认为你需要更改这一行:
if (!$this->upload->do_upload('files[]'))
至
if (!$this->upload->do_upload('files'))
试试这个:
$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;
并创建函数 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;
有关使用 codeigniter 上传多个文件的更多信息,请尝试此 http://w3code.in/2015/09/upload-file-using-codeigniter/
默认情况下,codeIgniter 不支持多文件上传。所以你可以使用
这个图书馆 CodeIgniter Multiple Upload Library
你没有通过文件名上传function.Try这个。
if (!$this->upload->do_upload($_FILES['files']['name']))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
请尝试以下
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);
}
}
您可以在单个请求中使用 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();
}
只是我从图书馆了解到 $this->upload->do_upload('inputelementname')
不必输入元素名称。应该是files数组的key。
所以在这种情况下这应该有效
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');
}
}
如何在 codeigniter 3.0.1 中上传多个文件。 Whosebug 中有类似的问题和解决方案,但不幸的是,没有一个能帮助解决我面临的问题。
这是 You did not select a file to upload
我当前代码
查看(添加画廊)
<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>
控制器(图库)
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;
}
我认为你需要更改这一行:
if (!$this->upload->do_upload('files[]'))
至
if (!$this->upload->do_upload('files'))
试试这个:
$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;
并创建函数 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;
有关使用 codeigniter 上传多个文件的更多信息,请尝试此 http://w3code.in/2015/09/upload-file-using-codeigniter/
默认情况下,codeIgniter 不支持多文件上传。所以你可以使用 这个图书馆 CodeIgniter Multiple Upload Library
你没有通过文件名上传function.Try这个。
if (!$this->upload->do_upload($_FILES['files']['name']))
{
$error =['error' => $this->upload->display_errors()];
$this->load->view('admin/addGallery', $error);
}
请尝试以下
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);
}
}
您可以在单个请求中使用 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();
}
只是我从图书馆了解到 $this->upload->do_upload('inputelementname')
不必输入元素名称。应该是files数组的key。
所以在这种情况下这应该有效
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');
}
}