今天应业务需要写了这个,留个备份以后用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php /** * Created by PhpStorm. * User: xiangming * Date: 2016/8/15 * Time: 17:14 */ class Qcode{ private $img; public function __construct(){ Header("Access-Control-Allow-Origin:*"); Header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE"); } public function index(){ $imgPath = isset($_POST['path'])?$_POST['path']:''; if(!$imgPath) $this->returnCode(-9,'地址为空'); $this->img = file_get_contents($imgPath); $ext = $this->getExt($imgPath); $path = '/upload/Qcode/'.date('Ymd').'_'.uniqid().'.'.$ext; $result = file_put_contents($_SERVER['DOCUMENT_ROOT'].$path,$this->img); if($result){ $this->returnCode(1,'图片保存成功','http://'.$_SERVER['SERVER_NAME'].$path); }else{ $this->returnCode(-1,'图片保存失败',$path); } } private function returnCode($code,$mes='',$data=''){ header('Content-Type:application/json; charset=utf-8'); exit(json_encode(['code'=>$code,'mes'=>$mes,'data'=>$data])); } private function getExt($file){ return pathinfo($file, PATHINFO_EXTENSION); } } $qcode = new Qcode(); $qcode->index(); |