public static function storeAttachment(\Phalcon\Http\Request\File $attachment) { $uploadDir = 'files'; //上传路径的设置 $time = time(); $path = static::makePath($uploadDir, $time); $ext = preg_replace('%^.*?(\\.[\\w]+)$%', "\$1", $attachment->getName()); //获取文件的后缀 $url = md5($attachment->getName()); $filename = $path . $time . $url . $ext; $attachment->moveTo($filename); return $filename; }
public function upload(File $file) { if ($file->getError()) { throw new Exception\IOException('ERR_FILE_UPLOAD_FAILED'); } $originalName = $file->getName(); $tmp = $file->getTempName(); $fileSize = $file->getSize(); $type = $file->getType(); $filenameArray = explode(".", $originalName); $fileExtension = strtolower(array_pop($filenameArray)); $originalFileName = implode('.', $filenameArray); $fileName = Tag::friendlyTitle($originalFileName); $fileHash = null; if ($fileName == '-') { $fileName = Text::random(Text::RANDOM_ALNUM, 6); } //hash file less then 10M if ($fileSize < 1048576 * 10) { $fileHash = hash_file('CRC32', $tmp, false); } if (false === strpos($type, 'image')) { $isImage = 0; } else { $isImage = 1; } $fileinfo = array('title' => $originalFileName, 'status' => 'published', 'storageAdapter' => 'local', 'originalName' => $originalName, 'fileSize' => $fileSize, 'mimeType' => $type, 'fileExtension' => $fileExtension, 'fileHash' => $fileHash, 'isImage' => $isImage, 'fileName' => $fileName . '.' . $fileExtension, 'createdAt' => time()); if ($isImage) { $image = getimagesize($tmp); $fileinfo['imageWidth'] = $image[0]; $fileinfo['imageHeight'] = $image[1]; } $filesystem = $this->getDI()->getFileSystem(); $path = md5(microtime()); $path = str_split($path, 2); $pathlevel = $this->getUploadPathLevel(); $pathlevel = $pathlevel > 6 ? 6 : $pathlevel; $path = array_slice($path, 0, $pathlevel); $filePath = implode('/', $path); $path = $filePath . '/' . $fileName . '.' . $fileExtension; $fileinfo['filePath'] = $filePath; $this->assign($fileinfo); if ($this->save()) { if (!$filesystem->has($path)) { if ($filesystem->write($path, file_get_contents($tmp))) { unlink($tmp); } else { throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED'); } } else { throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME'); } } else { throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED'); } return $this; }
/** * 新增专题 */ public function formAction() { //定义js匿名函数 $execJs = function ($msg) { $js = "<script type='text/javascript'>\r\n //禁止回退\r\n window.history.forward(1);\r\n alert('{$msg}');\r\n location.href = history.go(-1);\r\n </script>"; die($js); }; $validate = new \Validate(); $data['file_cat_id'] = $validate->getPost('file_cat_id', \Validate::int()); //专题类型 //验证参数 if ($validate->getMessage()) { $execJs('参数错误'); } //验证文件 if (!isset($_FILES['doc'])) { $execJs('非法操作'); } //验证zip压缩文件 $doc = new File($_FILES['doc']); if ($doc->getExtension() != 'doc' && $doc->getExtension() != 'docx' && $doc->getExtension() != 'ppt' && $doc->getExtension() != 'pptx') { $execJs('请上传doc、docx、ppt、pptx类型的文件'); } $file_name = $doc->getName(); //创建目录 $file_path = FILE_PATH . '/file/' . $data['file_cat_id']; $util = new \FileUtil(); $util->mkdir($file_path); $data['file_url'] = '/file/' . $data['file_cat_id'] . '/' . md5($file_name . time()) . '.' . $doc->getExtension(); $file_url = FILE_PATH . $data['file_url']; $data['file_name'] = $file_name; //移动资源文件,并重命名,与phtml文件相同 $doc->moveTo($file_url); $result = (new \File())->addFile($this->session->get('id'), $data); if ($result != 200) { //删除所有专题相关文件 $this->unlink($file_url); $execJs('上传失败'); } $execJs('专题添加成功'); }
/** * Check file allowed extensions * * @param \Phalcon\Http\Request\File $file * @param mixed $value * @return bool */ public function checkMimes(\Phalcon\Http\Request\File $file, $value) { //conversion to the desired format if (is_array($value) === false) { $value = [$value]; } if (in_array($file->getRealType(), $value) === false) { $this->errors[] = sprintf(Message::get('INVALID_MIME_TYPES'), $file->getName(), implode(',', $value)); return false; } return true; }
public function getName() { return parent::getName(); }
/** * Check maximum file size * * @param File $file * @param $value * @return bool * @throws \Exception */ public function checkMaxsize(File $file, $value) { if ($file->getSize() > (int) $value && $value !== null) { throw new \Exception(sprintf('The %s file is big. The maximum allowable %s', $file->getName(), $this->bytes($value))); } return true; }
/** * Validates the file extension * * @param UploadedFile $uploadedFile * * @return void * @throws Exception */ private function validateExtension(UploadedFile $uploadedFile) { $pathInfo = pathinfo($uploadedFile->getName()); if (!isset($pathInfo['extension'])) { throw new Exception('No Extension Found(' . $uploadedFile->getName() . ')', 401); } $extension = strtolower($pathInfo['extension']); if (!in_array($extension, $this->getAllowedExtensions())) { throw new Exception('Invalid Type of File Uploaded, valid types: ' . implode(', ', $this->getAllowedExtensions()), 401); } }