我使用codeigniter上传库上传多张图片。大多数情况下它工作正常,但在某些情况下图像无法上传。
功能:
function do_upload_img()
{
if(isset($_FILES['portfolioimg']['size']) != 0){
$name_array = array();
$files = $_FILES;
$cpt = count($_FILES['portfolioimg']['name']);
// $config['source_image'] = ''; //get original image
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = './assets/img/watermark.png';
$config['quality'] = 50;
$config['wm_vrt_alignment'] = 'middle';
$config['wm_hor_alignment'] = 'center';
$this->load->library('image_lib', $config);
for($i=0; $i<=$cpt-1; $i++)
{
$_FILES['userfile']['name']= $files['portfolioimg']['name'][$i];
$_FILES['userfile']['type']= $files['portfolioimg']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['portfolioimg']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['portfolioimg']['error'][$i];
$_FILES['userfile']['size']= $files['portfolioimg']['size'][$i];
$imgnam = rand(10,1000000).time();
$ext = pathinfo($files['portfolioimg']['name'][$i], PATHINFO_EXTENSION);
if($this->upload->initialize($this->set_upload_options($imgnam))){
$data = $this->upload->do_upload();
$config['source_image'] = './assets/portfolio_img/' . 'Rookie_'.$imgnam.'.'.$ext;
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$name_array[] = $this->upload->data('file_name');
$this->create_thumbnail('./assets/portfolio_img/',$imgnam,$ext);
}else{
$this->session->set_flashdata('falsemsg','Only jpg, jpeg, png files are allowed to be uploaded.');
}
}
return $name_array;
}else{
return false;
}
}
private function set_upload_options($imgnam)
{
//upload an image options
$config = array();
$config['upload_path'] = './assets/portfolio_img/';
$config['allowed_types'] = 'jpeg|jpg|png|JPEG|JPG|PNG';
$config['max_size'] = '0';
$config['overwrite'] = true;
$config['file_name'] = 'Rookie_'.$imgnam;
return $config;
}
这是我用来上传图片的功能。我不知道为什么它有时不起作用。所以请帮我弄清楚代码中的错误。提前谢谢。
答案 0 :(得分:1)
$b='portfolioimg';
$fileName = $_FILES[$b]['name'];
$images = $fileName;
$cpt = count($files);
for($i=1; $i<=$cpt; $i++)
{
$_FILES[$b]['name']= $files[$b]['name'][$i];
$_FILES[$b]['type']= $files[$b]['type'][$i];
$_FILES[$b]['tmp_name']= $files[$b]['tmp_name'][$i];
$_FILES[$b]['error']= $files[$b]['error'][$i];
$_FILES[$b]['size']= $files[$b]['size'][$i];
$uploadPath = './assets/portfolio_img/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|ico';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload($b.$i)){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$im[$i]=$uploadData[$i]['file_name'];
}
}
$tblValues = array(
'image'=>$images, // change field name,
);
$this->db->insert('table_name', $tblValues);
答案 1 :(得分:1)
控制器:
if(isset($_POST['submit']))
{
$config['upload_path']="./uploads/";
$config['allowed_types']='jpg|jpeg|gif|png';
$this->load->library('upload',$config);
if($this->upload->do_upload())
{
echo "file upload success";
}
else
{
echo "file upload failed";
}
}
查看:
<form action="<?php echo base_url();?>controller/function" method="POST" enctype="multipart/form-data" >
Select File To Upload:<br />
<input type="file" name="userfile" />
<br /><br />
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>