我想在成功将一些数据插入数据库后上传文件,但file uploading
不起作用。
以下是我的文件上传代码,但它没有上传。
控制器
public function saveReceipt(){
$doc=$this->receipt_m->saveReceipt_m();
if($doc){
$countfiles = count($_FILES['attatchments']['name']);
for($i=0;$i<$countfiles;$i++){
if(!empty($_FILES['attatchments']['name'][$i])){
// Define new $_FILES array - $_FILES['file']
$_FILES['file']['name'] = $_FILES['attatchments']['name'][$i];
$_FILES['file']['type'] = $_FILES['attatchments']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['attatchments']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['attatchments']['error'][$i];
$_FILES['file']['size'] = $_FILES['attatchments']['size'][$i];
// Set preference
$config['upload_path'] = base_url("assets/attachments");
$config['allowed_types'] = 'pdf|txt';
$config['max_size'] = '5000'; // max_size in kb
$config['file_name'] = $_FILES['attatchments']['name'][$i];
//Load upload library
$this->load->library('upload',$config);
$arr = array('msg' => 'something went wrong', 'success' => false);
// File upload
if($this->upload->do_upload('file')){
$data = $this->upload->data();
$arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);
}
}
}
echo json_encode($arr);
}else{
$response=array("status"=>false);
}
}
查看
<form class="horizontal-form" name="frmSaveReceipt" id="frmSaveReceipt" enctype="multipart/form-data">
<input type="file" name="attatchments[]" id="file" multiple="multiple">
</form
使用move_uploaded_file((上传多个文件。。。。
$filename = array();
for($i=0;$i<count($_FILES['attatchments']['name']);$i++){
if(!empty($_FILES['attatchments']['name'][$i])){
move_uploaded_file($_FILES['attatchments']['tmp_name'][$i],
'assets/attachments'.$_FILES['attatchments']['name'][$i]);
$filename[$i] = $_FILES['attatchments']['name'][$i];
}
}
现在使用$filename上的循环来上传数据库中的多个文件。。
下面的代码对我有效,请尝试一下控制器代码低于
$info=$this->input->post();
$data = array();
// If file upload form submitted
$countfiles = count($_FILES['galleryImage']['name']);
for($i=0;$i<$countfiles;$i++){
if(!empty($_FILES['galleryImage']['name'][$i])){
// Define new $_FILES array - $_FILES['file']
$_FILES['file']['name'] = $_FILES['galleryImage']['name'][$i];
$_FILES['file']['type'] = $_FILES['galleryImage']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['galleryImage']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['galleryImage']['error'][$i];
$_FILES['file']['size'] = $_FILES['galleryImage']['size'][$i];
// Set preference
$config['upload_path'] = './images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5000'; // max_size in kb
$config['file_name'] = $_FILES['galleryImage']['name'][$i];
//Load upload library
$this->load->library('upload',$config);
// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
// Initialize array
//$data['filenames'][] = $filename;
$color=array(
"pageId"=>$info["pageId"],
"meta_key"=>"gImage",
"meta_value"=>$filename,
"status"=>1
);
$insert=$this->pm->insert_data($table,$color);
}
}
}
视图代码为
<?php
$url=site_url('pages/SavePageAttribute');
$options = array('class' => 'form-inline', 'id' => 'category', 'method' => 'post','style'=>'margin:10px');
echo form_open_multipart($url, $options);
?>
<tr>
<td>
<?php
echo form_label('Gallery Image', 'galleryImage'); ?></td>
<td>
<input class="form-control" type="file" name="galleryImage[]" id="galleryImage" multiple="true">
</td>
</tr>
<?php echo form_close(); ?>