我想使用codeigniter将文件上传到本地文件夹,生成多个表单和文件输入字段,这些字段具有自己的id,当用户点击任何一个时,那个特定字段的id必须从ajax然后它的名字将通过附加id来创建(例如。“input_image”+1将是input_image1现在我必须从控制器上传它我的代码如下:
查看
<script type="text/javascript">
function sendVideoData(frm_id)
{
var data = new FormData(document.getElementById("post_video_"+frm_id));
// make the AJAX request
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
data: data+'&form_id='+frm_id,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
return false;
}
</script>
<?php for($i=0;$i<=10 (or it can be more); $i++)
{
<form name="post_video" id="post_video_<?=$row1['id']?>" method="post" onsubmit="return sendVideoData(<?=$row1['id']?>)">
<input type="file" name="save_movie_<?=$row1['id']?>" id="movie_<?=$row1['id']?>" />
<input name="type_lecture_id" class="get_lecture_id" id="get_lecture_id_<?=$row1['id']?>" value="<?=$row1['id']?>" type="hidden"/>
<input type="button" class="postbtn" id="submit_movie_<?=$row1['id']?>" value="Upload Video File"/>
</form>
}
控制器:
function do_upload()
{
$formid=$_POST['form_id'];
$file_name = 'save_movie_'.$formid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = FALSE;
$this->upload->do_upload($file_name);
$this->load->library('upload', $config);
}
上传文件夹位于根目录
中答案 0 :(得分:0)
在控制器中使用ajax获取文件名后,请执行此操作
public function file_upload(){
$formid=$_POST['form_id'];
$file_name = 'save_movie_'.$formid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = FALSE;
$this->upload->do_upload($file_name)
$this->load->library('upload', $config);
}
这适用于多次上传和单次上传,以便更多阅读此http://w3code.in/2015/09/upload-file-using-codeigniter/
答案 1 :(得分:0)
试试这个,
if (empty($_FILES['imagefile']['name'][0])) {
return FALSE;
}