| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 | <?php/** * upload文件浏览器 * * @version      0.0.0 * @author      by huwhois * @time        2017/11/27 */namespace app\admin\controller;use think\App;use think\Request;use think\facade\View;use think\Image;class FileManager extends Base{    protected $img_path = '';     protected $t_suffix = '_thumb';    protected $t_width = 400;    protected $t_height = 300;    protected $request;    public function uploadimg($thumb=false, $width=400, $height=300)    {        if ($this->request->isPost()) {            $file = $this->request->file('upload_file');            if ($file) {                try {                    validate(                        [                            'file' => [                                // 限制文件大小(单位b),这里限制为20M                                'fileSize' => 20 * 1024 * 1024,                                // 限制文件后缀,多个后缀以英文逗号分割                                'fileExt'  => 'jpg,png,gif,jpeg,webp'                            ]                        ],                        [                            'file.fileSize' => '文件太大',                            'file.fileExt' => '不支持的文件后缀',                        ]                    )->check(['file' => $file]);                    $savename = \think\facade\Filesystem::disk('public')->putFile( '/', $file);                    $thumbname = "";                    if ($thumb) {                        $thumbname = $this->makeThumb($savename, $width, $height);                    }                } catch (\think\exception\ValidateException $e) {                    $this->error($e->getMessage());                }                unset($file);                return [                    'code'=>2,                    'picname' => '/storage/' . str_replace('\\', '/', $savename),                    'thumbname'=>'/storage/' . $thumbname                ];            } else {                $this->error('图片不能为空');              }        } else {            View::assign('thumb', $thumb);            View::assign('width', $width);            View::assign('height', $height);            return View::fetch();        }    }    /**     * 生成缩略图     * @param string $filename 必须参数, 图片路径名(相对or绝对)(/public/uploads/...)     * @param int $width 缩略图宽值, 默认 384px;     * @param int $height 缩略图高值, 默认 224px;     * @param int $type 缩略图裁剪方式, 默认值 1, 固定尺寸缩放; 其他: 1, 等比例缩放;     * 2, 缩放后填充; 3, 居中裁剪; 4, 左上角裁剪; 5, 右下角裁剪     * @return string $thumbname 缩略图文件名     */    public function makeThumb($filename, $width =384, $height = 224, $type = 1)    {        $file ='./storage/' . str_replace('\\', '/', $filename);                $ext = pathinfo($file, PATHINFO_EXTENSION);        $thumbname = str_replace('.' . $ext, '', str_replace('\\', '/', $filename)) . $this->t_suffix . '.' . $ext;        $image = Image::open($file);        $result = $image->thumb($width, $height, $type)->save('./storage/' . $thumbname);        return $result ? $thumbname : '';    }    public function index()    {        $data = $this->explorer();        View::assign('dirs', $data['dirs']);        View::assign('files', $data['files']);        View::assign('counts', $data['counts']);        View::assign('activeurl', $data['activeurl']);        View::assign('activepath', $data['activepath']);        return View::fetch();    }    /**     * 获取当前文件相关信息     * @return array $data 文件信息数据组     */    protected function explorer()    {        $param = $this->request->param();         $activepath =  isset($param['activepath'])? $param['activepath'] : '';                $inpath = "";        $inpath = $this->img_path . $activepath;        $dirhandle = scandir($inpath);               $dirs = $files = [];        define('BKM', 1024);                foreach ($dirhandle as $val) {            if ($val == "." || $val == "..") {                continue;            } elseif (is_dir($inpath . DIRECTORY_SEPARATOR . $val)) {                $dirs[] = $val;            } else {                $arr = [];                $file = '';                $file = $inpath . DIRECTORY_SEPARATOR . $val;                                $arr['name'] = $val;                                $arr['extension'] = pathinfo($file, PATHINFO_EXTENSION);                $filesize = floatval(filesize($file));                if ($filesize>BKM) {                    $filesize =  round($filesize/BKM, 2);                    if ($filesize>BKM) {                        $filesize = round($filesize/BKM, 2);                                            $filesize .="M";                    } else {                        $filesize .="K";                    }                } else {                    $filesize .="B";                }                $arr['size'] = $filesize;                $filetime = filemtime("$file");                $arr['time'] = date("Y-m-d H:i:s", $filetime);                $files[] = $arr;            }        }        $counts = count($dirs)+count($files);        $activeurl = preg_replace("#[\/][^\/]*$#i", "", $activepath);               $data = [            'dirs'      => $dirs,            'files'     => $files,            'counts'    => $counts,            'activeurl' => $activeurl,            'activepath'=> $activepath,        ];        return $data;    }    public function delDir()    {        if ($this->request->isAjax()) {             $activepath = $this->request->param('activepath');            $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);            $dir = $this->request->param('dir');            $dir_name = app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$dir;                if (count(scandir($dir_name)) > 2) {                return ['status'=>1,'msg'=>'不可删除非空目录'];            }            if (rmdir($dir_name)) {                return ['status'=>2,'msg'=>'success'];            } else {                return ['status'=>0,'msg'=>'failed'];            }        }    }        public function del()    {        if ($this->request->isAjax()) {            $activepath = $this->request->param('activepath');            $activepath = str_replace('/', DIRECTORY_SEPARATOR, $activepath);            $filename = $this->request->param('filename');            if (unlink(app()->getRootPath.'public'.DIRECTORY_SEPARATOR.$activepath.DIRECTORY_SEPARATOR.$filename)) {                return ['status'=>1,'msg'=>'success'];            } else {                return ['status'=>0,'msg'=>'failed'];            }        }    }    // public function upload()    // {    //     if ($this->request->isGet()) {    //         return View::fetch()();    //     } else {    //         $isthumb = $this->request->has('isthumb', 'post') ? $this->request->post('isthumb') : 0;    //         $mode = $this->request->has('formername', 'post') ? $this->request->post('formername') : 0;    //         $file = request()->file('image');    //         $info = $this->saveUpload($file, $mode);    //         if (!$info) {    //             $this->error($info->getError());    //         }       //         $filename = $info->getSaveName();    //         unset($info);    //         if ($isthumb == 1) {    //             $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;    //             $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;                    //             $thumbname = $this->makeThumb($filename, $width, $height);    //             if (!$thumbname) {    //                 $this->error('缩略图生成失败');    //             }    //         } else {    //             $thumbname = '';    //         }    //         if ($this->request->isAjax()) {    //             return ['code'=>2, 'picname' => $filename, 'thumbname'=>$thumbname];    //         } else {    //             $this->success('上传成功', '/file_manager/index');    //         }    //     }    // }    /**     * 保存上传的图片     * @param object $file 获取的上传对象     * @param boolean $mode 是否保存原文件名 0(false), 生成新名称; 1(true), 保留原名     * @return object 返回文件保存对象     */    protected function saveUpload($file, $mode)    {        $validate = ['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'];        if ($mode) {            $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads', '');        } else {            $upload = $file->validate($validate)->move(app()->getRootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads');        }        return $upload;    }    /**     * 站内选择     * @return void     */    public function selectPicture()    {        $data = $this->explorer();        View::assign('data', $data);        return View::fetch()();    }    public function uploadPicture()    {        return View::fetch()();    }    // public function onlinePicture()    // {    //     if ($this->request->isAjax()) {    //         $param = $this->request->param();    //         $urlimg = $param['filename'];    //         $download = $param['download'];    //         $isthumb = $param['isthumb'];    //         $formername = $param['formername'];    //         $res = \mylib\GetImageByurl::isImg($urlimg);    //         if (!$res) {    //             $this->error('该图片不合法');    //         } else {    //             if (!$download) {    //                 return ['code'=>2,'filename'=>$urlimg];    //             } else {    //                 $basepath = \config('url_domain_root') . '/uploads';    //                 // var_dump($param);                        //                 if ($isthumb!=1  &&  preg_match("#" . $basepath . "#i", $urlimg)) {    //                     $this->error('图片已在服务其中, 可直接选用');    //                 }    //                 // 按文件夹日期夹存放图片    //                 $today = date('Ymd', time());    //                 $savePath = $this->img_path . $today;    //                 if (!is_dir($savePath)) {    //                     if (mkdir($savePath) == false) {    //                         $this->error('下载失败, 请稍后重试');    //                     }    //                 }                        //                 if ($formername==1) {    //                     // 获取原文件名    //                     $fileinfo = pathinfo($urlimg);    //                     if (!isset($fileinfo['extension']) || !in_array($fileinfo['extension'], ['jpg', 'jpeg', 'png', 'gif'])) {    //                         $ext = \mylib\GetImageByurl::getEXTENSION($urlimg);    //                     }    //                     $filename = $fileinfo['basename'] . '.' . $ext;    //                 } else {    //                     $filename = '';    //                 }                                //                 $filename = \mylib\GetImageByurl::getImageByurl($urlimg, $filename, $savePath);                        //                 if ($filename) {    //                     // 生成缩略图    //                     if ($isthumb==1) {    //                         $width = $this->request->has('width', 'post') ? $this->request->post('width') : $this->t_width;    //                         $height = $this->request->has('height', 'post') ? $this->request->post('height') : $this->t_height;                                //                         $thumbname = $this->makeThumb($today . DIRECTORY_SEPARATOR . $filename, $width, $height);    //                         if (!$thumbname) {    //                             $this->error('缩略图生成失败');    //                         }    //                     } else {    //                         $thumbname = '';    //                     }    //                     return ['code'=>2, 'filename' => $today . DIRECTORY_SEPARATOR . $filename, 'thumbname'=>$thumbname];    //                 } else {    //                     $this->error('图片下载失败');    //                 }    //             }    //         }       //     } else {    //         return View::fetch()();    //     }    // }    /**     * 判断(远程)文件是否为图片     */    // public function isImg()    // {    //     if ($this->request->isAjax()) {    //         $filename = $this->request->param('filename');    //         $res = \mylib\GetImageByurl::isImg($filename);    //         if (!$res) {    //             $this->error('该图片不合法');    //         } else {    //             return ['code'=>2,'msg'=>'图片地址可用'];    //         }    //     }    // }}
 |