PHP图片上传方法

时间:2021-10-17 10:01:50
/**
* 验证图片
* @params array $images,int $id,string $poster_path
* @return array
*/
function checkImg($images,$id,$poster_path) {
$msg = '';
$path = '';
//验证图片大小
if(isset($images) && $images['error'] == 0) {
if($images['size']>1024*700){
$msg = '图片不能超过700K';
}else {
$type = pathinfo($images['name'], PATHINFO_EXTENSION);
if ($type != "png" && $type != 'jpg' && $type != 'jpeg') {
$msg = "请上传正确格式的图片";
}
$time = time();
$file = "poster_{$time}.$type";
$path = "upload/junior/poster";
$upload = ROOT_PATH . "/Html/{$path}/{$file}";
if(!is_dir(ROOT_PATH . "/Html/{$path}")) {
mkdir(ROOT_PATH . "/Html/{$path}", 0777, true);
}
move_uploaded_file($images['tmp_name'], $upload);
$path = "{$path}/{$file}";
}
}elseif($id != 0){
$path = $poster_path;
}else {
$msg = '图片不能为空且图片大小不能超过700K';
}
return $res = array('msg' => $msg , 'path' => $path);
}