我有一个多部分表格。我想使用此表单上传5张图片和一个视频。我不想使用AJAX上传。
$this->form_validation->set_rules('file' , 'lang:pic' , 'callback_multiple_upload');
$this->form_validation->set_rules('video' , 'lang:video' , 'callback_video_upload');
答案 0 :(得分:2)
对于多个图像
$config['upload_path'] = PATH; //add path according to your requirements
$config['allowed_types'] = 'jpg|jpeg|png';
$config['overwrite'] = false; //OR true
$config['max_size'] = '100000'; //You can change it
$this->load->library('upload');
$files = $_FILES;
$number_of_files = count($_FILES['pic']['name']); //"pic" is name of FILE input
//images name will be details0, details1, details2 and soo on.
$errors = 0;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['pic']['name'] = "details" . $i . ".jpg"; //If you want to change the name of images change "details" with your require name
$_FILES['pic']['type'] = $files['pic']['type'][$i];
$_FILES['pic']['tmp_name'] = $files['pic']['tmp_name'][$i];
$_FILES['pic']['error'] = $files['pic']['error'][$i];
$_FILES['pic']['size'] = $files['pic']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload("pic"))
{
$errors++;
}
}
if ($errors > 0)
{
echo $errors . "File(s) could not be uploaded";
}
答案 1 :(得分:0)
您可以初始化每个文件的上传类,并设置所需的文件类型,即
$config['upload_path']= './uploads/files/';
$config['allowed_types'] = 'docx|doc|pdf|txt|odt';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
$this->upload->do_upload();
//check errors in first upload.
然后是视频
$config['upload_path']= './uploads/videos/';
$config['allowed_types'] = 'mp4|flv';
$config['max_size'] = 10000;
$this->upload->initialize($config);
$this->upload->do_upload();
答案 2 :(得分:0)
用于多文件上传
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$upload_path ="/test/assets/upload";
$output_dir = $_SERVER['DOCUMENT_ROOT']. $upload_path;
$config['upload_path'] = $output_dir;
// $config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|png|mp4; //add other_extensions';
$config['file_name'] = 'PIC_'.$user_id.'_'.$i;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$img_insert_array = array('user_id'=>$user_id,'profile_pic'=> $fileData['file_name']);
$res = $this->Test_model->update_img($img_insert_array );
//here insert in db
echo "uploaded successful";
}
else {
print_r($this->upload->display_errors());
}
}