当前位置 : 主页 > 网络编程 > PHP >

单张图片上传

来源:互联网 收集:自由互联 发布时间:2021-06-28
使用layer的单张图片上传 public function upload() { if ($_FILES["file"]["error"] 0){ $result = [ 'state' = 0, 'msg' = '上传失败', 'filename' = '', ]; }else{ $time=date ('Y-m-d',time ()); $uploaddir = str_replace ('\\', '/', 'upl
使用layer的单张图片上传
public function upload() {
        if ($_FILES["file"]["error"] > 0){
            $result = [
                'state'    => 0,
                'msg'      => '上传失败',
                'filename' => '',
            ];
        }else{
            $time=date ('Y-m-d',time ());
            $uploaddir = str_replace ('\\', '/', 'uploads/'.$time.'/');
            is_dir ($uploaddir)||mkdir ($uploaddir,0777,true);
            //获取当前图片的格式
            $type = strrchr ($_FILES["file"]['name'], '.');
            //将当前时间戳和图片格式拼接起来,形成图片路径
            $fileDate   = time ().$type;
            $uploadfile = $uploaddir.$fileDate;
            if (move_uploaded_file ($_FILES['file']['tmp_name'], $uploadfile)){
                $result = [
                    'state'    => 1,
                    'msg'      => '上传成功',
                    'filename' => $uploadfile,
                ];
            }else{
                $result = [
                    'state'    => 0,
                    'msg'      => '上传失败',
                    'filename' => $uploadfile,
                ];
            }
        }

        return json ($result);
    }
上一篇:2017.10.9
下一篇:数据库查询语句构造类
网友评论