Ajax-php 图片上传(已整理)

/**
 *注意,一共有三个地址,假设图片存储地址为“../../AAA/BBB/CCC.jpg”,1、后端定死“../../AAA”,2、前端传过来“/BBB”,后端生成“/CCC.jpg”
 *本接口,图片信息使用post传值,但path参数无法携带,故使用get传path;若您有其他好办法,还望留言相告
 */


//1、封装接口类
class uploadImageAjax{
    private $uptypes =[//上传文件类型列表
        'image/jpg',
        'image/jpeg',
        'image/png',
        'image/pjpeg',
        'image/gif',
        'image/bmp',
        'image/x-png'
    ];
    private $base_name = "/weixinpl/up";
    public $max_upload_size = 2;//2M
    private $save_path = "../../../up";//图片存储路径,基准根路径
    private $path = "";//上传文件存储的文件地址
    private $name = "";//本地文件名称
    private $type = "";//文件类型
    private $tmp_name = "";//上传后临时文件名称
    private $error = "";//错误信息
    private $size = "";//文件大小
    private $new_file_name = "";//新文件名称
    function __construct($file,$path)
    {
        $this->path = $path;
        $this->save_path = $this->save_path.$path;//拼接保存地址
        if(!file_exists($this->save_path)){//文件夹不存在,创建文件夹
            mkdir($this->save_path);
        }
        $this->name = $file['name'];
        $this->type = $file['type'];
        $this->tmp_name = $file['tmp_name'];
        $this->error = $file['error'];
        $this->size = $file['size'];
        $this->new_file_name = "/".time().".".pathinfo($file["name"])['extension'];
    }

    /**
     * 检测文件是否通过HTTP上传
     * @return bool
     */
    public function check_source(){
        if(!is_uploaded_file($this->tmp_name)){
            return false;
        }
        return true;
    }

    /**
     * 检测文件类型是否允许上传
     * @return bool
     */
    public function check_type(){
        if(!in_array($this->type, $this->uptypes)){
            return false;
        }
        return true;
    }

    /**
     * 验证文件大小
     * @return bool
     */
    public function check_size(){
        if($this->size > $this->max_upload_size*1024*1024){
            return false;
        }
        return true;
    }

    /**
     * 移动临时文件到指定文件夹
     * @return bool|string
     */
    public function upload_image(){
        if(!move_uploaded_file($this->tmp_name, $this->save_path.$this->new_file_name)){
            return false;
        }
        return $this->base_name.$this->path.$this->new_file_name;//返回完整路径
    }
    function __destruct()
    {
        // TODO: Implement __destruct() method.
    }
}
//2、实例化调用
$model = new uploadImageAjax($_FILES['file'],$_GET['path']);
if(!$model->check_source()){
    echo json_encode([
        'code' => -1,
        'msg' => '文件不存在',
    ]);die;
}
if(!$model->check_type()){
    echo json_encode([
        'code' => -2,
        'msg' => '不允许上传此类型文件',
    ]);die;
}
if(!$model->check_size()){
    echo json_encode([
        'code' => -3,
        'msg' => "文件不允许超过{$model->max_upload_size}M",
    ]);die;
}
$result = $model->upload_image();
if($result){
    echo json_encode([
        'code' => 1,
        'msg' => '上传成功',
        'path' => $result
    ]);die;
}else{
    echo json_encode([
        'code' => -4,
        'msg' => '上传失败',
    ]);die;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值