我只在有任何输入值时调用do_upload_img()函数,如下所示:
function xxx()
{
if($_FILES['portfolioimg']['size'])
{
$portfolioimg = $this->do_upload_img();
}
// remaining code.......
}
do_upload_img()函数
function do_upload_img()
{
if(isset($_FILES['portfolioimg']['size']) != 0){
$name_array = array();
$files = $_FILES;
$cpt = count($_FILES['portfolioimg']['name']);
$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';
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);
$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/' . 'Soooo_'.$imgnam.'.'.$ext;
$this->image_lib->initialize($config);
$this->image_lib->watermark();
$name_array[] = $this->upload->data('file_name');
} else {
$this->session->set_flashdata('falsemsg','Only jpg, jpeg, png files are allowed to be uploaded.');
}
}
return $name_array;
}else{
return false;
}
}
查看
<input class="fileUpload" accept="image/jpeg, image/jpg, image/png, image/gif" name="portfolioimg[]" type="file" value="" data-msg-accept="Please upload only jpg, jpeg, png and gif file">
如果我运行此 xxx()函数,并且portfolioimg
输入字段没有值,则会显示如下警告消息:
遇到PHP错误 严重性:警告
消息:imagecopy()期望参数1是资源,布尔
给出
文件名:libraries / Image_lib.php
行号:1217
如果我使用 error_reporting(0),仍然会显示警告。