示例#1
0
 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
 /**
  * Tests getRealType
  *
  * @issue  1442
  * @author Serghei Iakovlev <*****@*****.**>
  * @author Dreamszhu <*****@*****.**>
  * @since  2013-10-26
  */
 public function testRealType()
 {
     if (!extension_loaded('fileinfo')) {
         $this->markTestSkipped('Warning: fileinfo extension is not loaded');
     }
     $this->specify("getRealType does not returns real type", function () {
         $file = new File(['name' => 'test', 'type' => 'text/plain', 'tmp_name' => PATH_DATA . '/assets/phalconphp.jpg', 'size' => 1, 'error' => 0]);
         expect($file->getType())->equals('text/plain');
         expect($file->getRealType())->equals('image/jpeg');
     });
 }
示例#3
0
 public function getType()
 {
     return parent::getType();
 }