UploadFile::upload PHP Method

upload() public method

+---------------------------------------------------------- 上传文件 +---------------------------------------------------------- +----------------------------------------------------------
public upload ( string $savePath = '' ) : string
$savePath string 上传文件保存路径 +----------------------------------------------------------
return string +----------------------------------------------------------
    public function upload($savePath = '')
    {
        mkdir($savePath, 0777, true);
        //如果不指定保存文件名,则由系统默认
        if (empty($savePath)) {
            $savePath = $this->savePath;
        }
        // 检查上传目录
        if (!is_dir($savePath)) {
            // 检查目录是否编码后的
            if (is_dir(base64_decode($savePath))) {
                $savePath = base64_decode($savePath);
            } else {
                // 尝试创建目录
                if (!mkdir($savePath, 0777, true)) {
                    $this->error = '上传目录' . $savePath . '不存在';
                    return false;
                }
            }
        } else {
            if (!is_writable($savePath)) {
                $this->error = '上传目录' . $savePath . '不可写';
                return false;
            }
        }
        $fileInfo = array();
        $isUpload = false;
        // 获取上传的文件信息
        // 对$_FILES数组信息处理
        $files = $this->dealFiles($_FILES);
        foreach ($files as $key => $file) {
            //过滤无效的上传
            if (!empty($file['name'])) {
                $file['key'] = $key;
                $file['extension'] = $this->getExt($file['name']);
                $file['savepath'] = $savePath;
                $file['savename'] = uniqid() . substr(str_shuffle('0123456789abcdef'), rand(0, 9), 7) . '.' . $file['extension'];
                //$this->getSaveName($file);
                if ($GLOBALS['fromMobile'] == true && empty($file['extension'])) {
                    //移动设备上传的无后缀的图片,默认为jpg
                    $file['extension'] = 'jpg';
                    $file['savename'] = trim($file['savename'], '.') . '.jpg';
                } else {
                    // 自动检查附件
                    if ($this->autoCheck) {
                        if (!$this->check($file)) {
                            return false;
                        }
                    }
                }
                //保存上传文件
                if (!$this->save($file)) {
                    return false;
                }
                if (function_exists($this->hashType)) {
                    $fun = $this->hashType;
                    $file['hash'] = $fun(auto_charset($file['savepath'] . $file['savename'], 'utf-8', 'gbk'));
                }
                //上传成功后保存文件信息,供其它地方调用
                unset($file['tmp_name'], $file['error']);
                $fileInfo[] = $file;
                $isUpload = true;
                //图片上传裁剪
                //$this->resetimg($savePath,$file['savename'],$file['save_Path']);
            }
        }
        if ($isUpload) {
            $this->uploadFileInfo = $fileInfo;
            return true;
        } else {
            $this->error = '上传出错!文件不符合上传要求。';
            return false;
        }
    }

Usage Example

 public function localupload()
 {
     $upload = new UploadFile();
     $upload->allowExts = array("pem");
     $upload->uploadReplace = 1;
     $firstLetter = substr($this->token, 0, 1);
     $upload->savePath = "./uploads/" . $firstLetter . "/" . $this->token . "/";
     if (!file_exists($_SERVER["DOCUMENT_ROOT"] . "/uploads") || !is_dir($_SERVER["DOCUMENT_ROOT"] . "/uploads")) {
         mkdir($_SERVER["DOCUMENT_ROOT"] . "/uploads", 511);
     }
     $firstLetterDir = $_SERVER["DOCUMENT_ROOT"] . "/uploads/" . $firstLetter;
     if (!file_exists($firstLetterDir) || !is_dir($firstLetterDir)) {
         mkdir($firstLetterDir, 511);
     }
     if (!file_exists($firstLetterDir . "/" . $this->token) || !is_dir($firstLetterDir . "/" . $this->token)) {
         mkdir($firstLetterDir . "/" . $this->token, 511);
     }
     if (!file_exists($upload->savePath) || !is_dir($upload->savePath)) {
         mkdir($upload->savePath, 511);
     }
     if (!$upload->upload()) {
         $error = 1;
         $msg = $upload->getErrorMsg();
         $this->error($msg);
         exit;
     } else {
         $error = 0;
         $info = $upload->getUploadFileInfo();
         $this->siteUrl = $this->siteUrl ? $this->siteUrl : C("site_url");
         $msg = $this->siteUrl . substr($upload->savePath, 1) . $info[0]["savename"];
         $this->addCert($info[0]["key"], $msg);
     }
 }
All Usage Examples Of UploadFile::upload