我有一个问题,使用codeigniter进行多个文件上传 这是我的看法
<input type="file" name="song_name[]" value="" id="song_name">
<input type="file" name="song_name[]" value="" id="song_name">
<input type="file" name="song_name[]" value="" id="song_name">
这个输入元素在form with multipart / formdata
我正在使用具有相同名称的jquery创建每个文本框 这是我上传文件的控制器代码
/* Conf */
$config['upload_path'] = './uploads/test/';
$config['allowed_types'] = 'gif|jpg|png|mov|mp3|aiff|mpeg|zip';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['file_ext_tolower'] = 'TRUE';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$filesCount = count($_FILES['song_name']['name']);
echo $filesCount;
print_r($_FILES['song_name']); exit;
for($i = 0; $i < $filesCount; $i++) {
$_FILES['userFile']['name'] = $_FILES['song_name']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['song_name']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['song_name']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['song_name']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['song_name']['size'][$i];
print_r($_FILES['userFile']);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')) {
echo "upload";
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
} else {
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
所以请帮我解决这个问题,让我知道如何使用此方法上传
答案 0 :(得分:1)
/**
* @param array $config
* @param string $file_input_name
* @param array $resize_configs
* @return array
* */
protected function multiple_upload($config = array(),$file_input_name,$resize_configs = null)
{
$fileNames = array();
$conf['upload_path'] = FCPATH . 'assets/img';
$conf['allowed_types'] = 'jpg|png|jpeg';
$conf['max_size'] = 50000;
$conf['max_width'] = 3600;
$conf['max_height'] = 1800;
$conf['maintain_ratio'] = TRUE;
$conf['encrypt_name'] = TRUE;
foreach ($config as $item => $val){
$conf[$item] = $val;
}
$this->load->library('upload',$conf);
$this->load->library('image_lib');
$files = $_FILES;
$cpt = sizeof($_FILES[$file_input_name]['name']);
for($i = 0; $i < $cpt; $i++)
{
$_FILES['userfile']['name'] = $files[$file_input_name]['name'][$i];
$_FILES['userfile']['type'] = $files[$file_input_name]['type'][$i];
$_FILES['userfile']['tmp_name'] = $files[$file_input_name]['tmp_name'][$i];
$_FILES['userfile']['error'] = $files[$file_input_name]['error'][$i];
$_FILES['userfile']['size'] = $files[$file_input_name]['size'][$i];
if($this->upload->do_upload()){
$fileNames[] = $this->upload->file_name;
if(!is_null($resize_configs)){
//Image Resizing
$img_config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
foreach($resize_configs as $key => $value){
$img_config[$key] = $value;
}
$this->image_lib->initialize($img_config);
$this->image_lib->resize();
}
}
}
return $fileNames;
}