UploadFile::getSaveName PHP Method

getSaveName() private method

+---------------------------------------------------------- 根据上传文件命名规则取得保存文件名 +---------------------------------------------------------- +----------------------------------------------------------
private getSaveName ( string $filename ) : string
$filename string 数据 +----------------------------------------------------------
return string +----------------------------------------------------------
    private function getSaveName($filename)
    {
        if ($this->saveName) {
            return $this->saveName;
        } else {
            $rule = $this->saveRule;
            if (empty($rule)) {
                //没有定义命名规则,则保持文件名不变
                $saveName = $filename['name'];
            } else {
                if (function_exists($rule)) {
                    //使用函数生成一个唯一文件标识号
                    $saveName = $rule() . '.' . $filename['extension'];
                } else {
                    //使用给定的文件名作为标识号
                    $saveName = $rule . '.' . $filename['extension'];
                }
            }
            if ($this->autoSub) {
                // 使用子目录保存文件
                $saveName = $this->getSubName($filename) . '/' . $saveName;
            }
            return $saveName;
        }
    }

Usage Example

 public function run()
 {
     $action = I('get.action');
     $result = array();
     switch ($action) {
         case 'config':
             $result = $this->confing;
             break;
             //上传涂鸦
         //上传涂鸦
         case 'uploadscrawl':
             $catid = I('get.catid');
             $module = I('get.module', $catid ? 'content' : MODULE_NAME, 'trim');
             $base64Data = $_POST[$this->confing['scrawlFieldName']];
             if (empty($base64Data)) {
                 exit(json_encode(array('state' => '没有涂鸦内容!')));
             }
             $img = base64_decode($base64Data);
             $oriName = 'scrawl.png';
             $fileType = 'png';
             $fileSize = strlen($img);
             //上传目录
             $savePath = D('Attachment/Attachment')->getFilePath($module, 'Y/m', time());
             $up = new \UploadFile();
             //保存文件名
             $fileName = $up->getSaveName(array('name' => $oriName, 'extension' => 'png'));
             //保存地址
             $filePath = $savePath . $fileName;
             //保存后的访问地址
             $url = self::$Cache['Config']['sitefileurl'] . str_replace(array(C('UPLOADFILEPATH'), '//', '\\'), array('', '/', '\\/'), $filePath);
             //写入临时文件
             if (file_put_contents($filePath, $img)) {
                 $result = array('state' => 'SUCCESS', 'url' => $url, 'title' => $oriName, 'original' => $oriName);
             } else {
                 exit(json_encode(array('state' => '保存失败!')));
             }
             break;
             //上传图片
         //上传图片
         case 'uploadimage':
             $catid = I('get.catid');
             $module = I('get.module', $catid ? 'content' : MODULE_NAME, 'trim');
             $Attachment = service('Attachment', array('module' => $module, 'catid' => $catid, 'userid' => $this->uid, 'isadmin' => $this->isadmin));
             //设置上传类型,强制为图片类型
             $Attachment->uploadallowext = array("jpg", "png", "gif", "jpeg");
             if ($this->isadmin < 1) {
                 //如果是非后台用户,进行权限判断
                 $member_group = cache('Member_group');
                 if ((int) $member_group[$this->groupid]['allowattachment'] < 1) {
                     exit(json_encode(array('state' => '没有上传权限!')));
                 }
             }
             //开始上传
             $info = $Attachment->upload();
             if ($info) {
                 // 设置附件cookie
                 $Attachment->upload_json($info[0]['aid'], $info[0]['url'], str_replace(array("\\", "/"), "", $info[0]['name']));
                 $result = array('state' => 'SUCCESS', 'url' => $info[0]['url'], 'title' => str_replace(array("\\", "/"), "", $pictitle ? $pictitle : $info[0]['name']), 'original' => $info[0]['name']);
             } else {
                 $result = array('state' => $Attachment->getError() ?: '上传失败');
             }
             break;
             //图片在线管理
         //图片在线管理
         case 'listfile':
         case 'listimage':
             $listArr = $this->att_not_used();
             $list = array();
             foreach ($listArr as $rs) {
                 if (!isImage($rs['src']) && $action != 'listfile') {
                     continue;
                 }
                 $list[] = array('url' => $rs['src'], 'mtime' => time());
             }
             $result = array('state' => 'SUCCESS', 'list' => $list, 'total' => count($listArr));
             break;
             //上传视频
         //上传视频
         case 'uploadvideo':
             //上传附件
         //上传附件
         case 'uploadfile':
             $catid = I('get.catid');
             $module = I('get.module', $catid ? 'content' : MODULE_NAME, 'trim');
             $Attachment = service('Attachment', array('module' => $module, 'catid' => $catid, 'userid' => $this->uid, 'isadmin' => $this->isadmin));
             //设置上传类型
             if ($this->isadmin) {
                 $Attachment->uploadallowext = explode('|', self::$Cache['Config']['uploadallowext']);
             } else {
                 $Attachment->uploadallowext = explode('|', self::$Cache['Config']['qtuploadallowext']);
             }
             //回调函数
             $Callback = false;
             if ($this->isadmin < 1) {
                 //如果是非后台用户,进行权限判断
                 $member_group = cache('Member_group');
                 if ((int) $member_group[$this->groupid]['allowattachment'] < 1) {
                     exit(json_encode(array('state' => '没有上传权限!')));
                 }
             }
             //开始上传
             $info = $Attachment->upload($Callback);
             if ($info) {
                 // 设置附件cookie
                 $Attachment->upload_json($info[0]['aid'], $info[0]['url'], str_replace(array("\\", "/"), "", $info[0]['name']));
                 $result = array('state' => 'SUCCESS', 'url' => $info[0]['url'], 'name' => str_replace(array("\\", "/"), "", $pictitle ? $pictitle : $info[0]['name']), 'size' => $info[0]['size'], 'type' => '.' . $info[0]['extension'], 'original' => $info[0]['name']);
             } else {
                 $result = array('state' => $Attachment->getError() ?: '上传失败');
             }
             break;
         default:
             $result = array('state' => '请求地址出错');
             break;
     }
     exit(json_encode($result));
 }