예제 #1
0
파일: Upload.php 프로젝트: skybird/phalcon
 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;
 }
예제 #2
0
 /**
  * Check maximum file size
  *
  * @param \Phalcon\Http\Request\File $file
  * @param mixed $value
  * @return bool
  */
 public function checkMaxsize(\Phalcon\Http\Request\File $file, $value)
 {
     //conversion to the desired format
     if (is_array($value) === true) {
         $value = $value[key($value)];
     }
     // check
     if ($file->getSize() > (int) $value) {
         $this->errors[] = sprintf(Message::get('INVALID_MAX_SIZE'), $file->getName(), Format::bytes($value));
         return false;
     }
     return true;
 }
예제 #3
0
파일: File.php 프로젝트: mattvb91/cphalcon
 public function getSize()
 {
     return parent::getSize();
 }
예제 #4
0
 /**
  * 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;
 }
예제 #5
0
 /**
  * Check maximum file size
  *
  * @param \Phalcon\Http\Request\File $file
  * @param mixed $value
  * @return bool
  */
 public function checkMaxsize(\Phalcon\Http\Request\File $file, $value)
 {
     $pass = true;
     if ($value !== null && is_numeric($value)) {
         if ($file->getSize() > (int) $value) {
             $pass = false;
         }
     }
     return $pass;
 }