Fork me on GitHub

php图片上传

//处理图片
private function imageDeal($param){
$arrType=array('image/jpg','image/bmp','image/png','image/jpeg');
$max_size=2*1024*1024;
if(!empty($param['name'])){
if($param['error']>0){
//echo "<script>alert('上传错误!');window.location.href='/index.php?m=member&c=sysrestr&a=index';</script>";
//echo 'aaaaaaaaaaaa';die;
$this->error('上传错误!','/Home/Baoming/oindex',2);
}else{
if(!in_array($param['type'],$arrType)){ //判断图片文件的格式
//echo "<font color='#FF0000'>上传文件格式不对!</font>";
// echo "<script>alert('上传文件格式不对!');window.location.href='/index.php?m=member&c=sysrestr&a=index';</script>";
//echo 'bbbbb';die;
$this->error('上传错误!','/Home/Baoming/oindex',2);
}else{
switch($param['type'])
{
case 'image/jpg':
$ext = '.jpg';
break;
case 'image/bmp':
$ext = '.bmp';
break;
case 'image/jpeg':
$ext = '.jpg';
break;
case 'image/png':
$ext = '.png';
break;
default:
//echo 'The file you uploaded was not a supported filetype.';
break;
}
}
$xt = md5(strtotime(date('Y-m-d H:i:s')).mt_rand(0,100));
if(file_exists("Baoming/images/upload/".$xt.$ext)){

$this->error('上传已经存在!','/Home/Baoming/oindex',2);
//return FALSE;
}else{
if(is_uploaded_file($param['tmp_name'])){
$stored_path = 'Baoming/images/upload/'.$xt.$ext;
//echo $stored_path ;die;

if(move_uploaded_file($param['tmp_name'],$stored_path)){
$imgurl = $stored_path;
}else{
//echo 'cccc';die;
$this->error('上传错误!','/Home/Baoming/oindex',2);

}
}else{
//echo 'ccdddddcc';die;
$this->error('上传错误!','/Home/Baoming/oindex',2);

}
}
}
}


return $imgurl;
}

 

然后调用即可。

posted @ 2016-03-04 17:44  我曾如此奢望  阅读(208)  评论(0编辑  收藏  举报