上传图片
public function images() { $data = $_FILES[‘file‘]; switch($data[‘type‘]) { case ‘image/jpeg‘: $ext = ‘jpg‘; break; case ‘image/gif‘: $ext = ‘gif‘; break; case ‘image/png‘: $ext = ‘png‘; break; default: $ext = ‘‘; break; } if($ext){ $file_name = uniqid().‘.‘.$ext; // 新的文件名 $tmp_name = $data[‘tmp_name‘]; // 服务器上临时文件名 $file_size = $data[‘size‘]; // 文件大小 if($file_size > 204800){ $result = array(‘stat‘=>‘-2‘,‘str‘=>‘图片大于200K‘); echo json_encode($result); exit; }else{ $file_path = ‘upload/‘.$file_name; $img_path = "http://aaaaaaaaa/upload/".$file_name;//表单用的 if(move_uploaded_file($tmp_name, $file_path) == true ){ $result = array(‘stat‘=>‘1‘,‘str‘=>‘添加图片成功‘,‘img‘=>$img_path,‘pic‘=>$file_name); }else{ $result = array(‘stat‘=>‘-2‘,‘str‘=>‘添加图片失败‘); } echo json_encode($result); exit; } }else{ $result = array(‘stat‘=>‘-2‘,‘str‘=>‘图片格式不对‘); echo json_encode($result); exit; } }
时间: 2024-10-11 01:15:25