Esempio n. 1
0
 public function upload()
 {
     $this->savePath = '/' . date('Ym');
     if (!is_dir($this->fileDir . $this->savePath) && !mkdir($this->fileDir . $this->savePath)) {
         throw new Exception(sprintf('目录 %s 不存在', $this->fileDir . $this->savePath), 403);
     }
     @chmod($this->fileDir . $this->savePath);
     if (!is_writeable($this->fileDir . $this->savePath)) {
         throw new Exception(sprintf('目录 %s 不可写', $this->fileDir . $this->savePath), 403);
     }
     foreach ($this->fileArr as $k => $file) {
         if (!self::isUploadedFile($file['tmp_name'])) {
             $this->resultArr[$k]['errno'] = 1;
             $this->resultArr[$k]['description'] = '文件上传失败';
             continue;
         }
         $suffix = Str::suffix($file['name']);
         $isImage = in_array($suffix, $this->allowImageExtArr);
         if ($this->onlyAllowImage && !$isImage) {
             $this->resultArr[$k]['errno'] = 2;
             $this->resultArr[$k]['description'] = '不允许上传非图片类型文件';
             continue;
         }
         if (!in_array($suffix, $this->allowFileExtArr)) {
             $this->resultArr[$k]['errno'] = 3;
             $this->resultArr[$k]['description'] = '类型文件不允许';
             continue;
         }
         if ($file['size'] > $this->maxFileSize) {
             $this->resultArr[$k]['errno'] = 4;
             $this->resultArr[$k]['description'] = '文件大小超过限制';
             continue;
         }
         $filename = date('d') . Str::random(6) . '.' . $suffix;
         $target = $this->fileDir . $this->savePath . '/' . $filename;
         if (move_uploaded_file($file['tmp_name'], $target) || @copy($file['tmp_name'], $target)) {
             $this->resultArr[$k]['errno'] = 0;
             $this->resultArr[$k]['description'] = '文件上传成功';
             $this->uploadedFileArr[] = array('name' => $file['name'], 'url' => $this->savePath . '/' . $filename, 'type' => $file['type'], 'size' => $file['size'], 'description' => $file['description'], 'is_image' => $isImage);
         } else {
             $this->resultArr[$k]['errno'] = 5;
             $this->resultArr[$k]['description'] = '文件上传失败';
         }
     }
     return $this;
 }