从动态表单上传 Codeigniter 多输入文件
Codeignitor multiple input file upload from a dynamic form
我有一个使用 Jquery 的动态表单(拖放表单),它有多个输入类型=文件。因此输入类型的名称使用随机数更改。以下是来自查看源代码的示例代码:
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div id="sjfb-fields">
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-399480" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-399480" style="color:grey;font-size:12px">Upload the screenshot</label>
<input type="file" name="img-399480" id="images-399480">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-857945" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-857945" style="color:grey;font-size:12px">Test Images</label>
<input type="file" name="img-857945" id="images-857945">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-792565" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-792565" style="color:grey;font-size:12px">More Images</label>
<input type="file" name="img-792565" id="images-792565">
</div>
</div>
</div>
<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>
我在控制器中使用了以下代码来上传一张图片:
$config['upload_path'] = 'public/Uploads/Inspection/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
我已经尝试使用一种输入类型上传="file"并且它有效(使用图像的名称用户文件)。
此表单有多个输入类型=文件,我想知道有什么方法可以单独上传文件吗?
我也是 codeigniter 的新手
谢谢!
看看这个:
控制器部分:
$files = $_FILES; //getting the files from post
$cpt = count($_FILES['photo']['name']); //number of files uploaded
for($i=0;$i<$cpt;$i++){ // in loop to upload multiple files
$file_name=$_FILES['photo']['name'][$i];
if($i==0){ //name the file according to the number /name as u like
echo $i;
$name='_first';
}elseif($i==2){
$name='_second';
}else{
$name='_third';
}
//ci图库上传照片
$this->load->library('upload');
$_FILES['photo']['name']= $files['photo']['name'][$i];
$_FILES['photo']['type']= $files['photo']['type'][$i];
$_FILES['photo']['tmp_name']= $files['photo']['tmp_name'][$i];
$_FILES['photo']['error']= $files['photo']['error'][$i];
$_FILES['photo']['size']= $files['photo']['size'][$i];
$config = array(
'file_name' => $name,
'allowed_types' => 'jpg', //add option as u like
'max_size' => 3000,
'overwrite' => TRUE,
'upload_path'=>'./uploads/' //use your respective path to upload
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo')) {
$data_error = array('msg' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
}
}
并在 html 中填写表格以上传照片:所有
的输入名称已更改为照片[]
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div id="sjfb-fields">
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-399480" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-399480" style="color:grey;font-size:12px">Upload the screenshot</label>
<input type="file" name="photo[]" id="images-399480">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-857945" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-857945" style="color:grey;font-size:12px">Test Images</label>
<input type="file" name="photo[]" id="images-857945">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-792565" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-792565" style="color:grey;font-size:12px">More Images</label>
<input type="file" name="photo[]" id="images-792565">
</div>
</div>
</div>
<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>
在本地工作
查看此 https://www.codeigniter.com/user_guide/libraries/file_uploading.html 配置选项
我有一个使用 Jquery 的动态表单(拖放表单),它有多个输入类型=文件。因此输入类型的名称使用随机数更改。以下是来自查看源代码的示例代码:
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div id="sjfb-fields">
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-399480" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-399480" style="color:grey;font-size:12px">Upload the screenshot</label>
<input type="file" name="img-399480" id="images-399480">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-857945" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-857945" style="color:grey;font-size:12px">Test Images</label>
<input type="file" name="img-857945" id="images-857945">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-792565" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-792565" style="color:grey;font-size:12px">More Images</label>
<input type="file" name="img-792565" id="images-792565">
</div>
</div>
</div>
<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>
我在控制器中使用了以下代码来上传一张图片:
$config['upload_path'] = 'public/Uploads/Inspection/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
我已经尝试使用一种输入类型上传="file"并且它有效(使用图像的名称用户文件)。 此表单有多个输入类型=文件,我想知道有什么方法可以单独上传文件吗? 我也是 codeigniter 的新手
谢谢!
看看这个:
控制器部分:
$files = $_FILES; //getting the files from post
$cpt = count($_FILES['photo']['name']); //number of files uploaded
for($i=0;$i<$cpt;$i++){ // in loop to upload multiple files
$file_name=$_FILES['photo']['name'][$i];
if($i==0){ //name the file according to the number /name as u like
echo $i;
$name='_first';
}elseif($i==2){
$name='_second';
}else{
$name='_third';
}
//ci图库上传照片 $this->load->library('upload');
$_FILES['photo']['name']= $files['photo']['name'][$i];
$_FILES['photo']['type']= $files['photo']['type'][$i];
$_FILES['photo']['tmp_name']= $files['photo']['tmp_name'][$i];
$_FILES['photo']['error']= $files['photo']['error'][$i];
$_FILES['photo']['size']= $files['photo']['size'][$i];
$config = array(
'file_name' => $name,
'allowed_types' => 'jpg', //add option as u like
'max_size' => 3000,
'overwrite' => TRUE,
'upload_path'=>'./uploads/' //use your respective path to upload
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('photo')) {
$data_error = array('msg' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
}
}
并在 html 中填写表格以上传照片:所有
的输入名称已更改为照片[]<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<form action="" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div id="sjfb-fields">
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-399480" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-399480" style="color:grey;font-size:12px">Upload the screenshot</label>
<input type="file" name="photo[]" id="images-399480">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-857945" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-857945" style="color:grey;font-size:12px">Test Images</label>
<input type="file" name="photo[]" id="images-857945">
</div>
</div>
<div class="my-3 p-3 bg-white rounded box-shadow">
<div id="sjfb-792565" class="sjfb-field sjfb-images" style="min-height:100px;">
<label for="images-792565" style="color:grey;font-size:12px">More Images</label>
<input type="file" name="photo[]" id="images-792565">
</div>
</div>
</div>
<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>
在本地工作 查看此 https://www.codeigniter.com/user_guide/libraries/file_uploading.html 配置选项