Пример #1
0
 public function testIsInvalidIfNotUploadedFile()
 {
     \Upload\FileInfo::setFactory(function ($tmpName, $name) {
         $fileInfo = $this->getMock('\\Upload\\FileInfo', array('isUploadedFile'), array($tmpName, $name));
         $fileInfo->expects($this->any())->method('isUploadedFile')->will($this->returnValue(false));
         return $fileInfo;
     });
     $file = new \Upload\File('single', $this->storage);
     $this->assertFalse($file->isValid());
 }
Пример #2
0
 /**
  * Constructor
  *
  * @param  string                    $key     The $_FILES[] key
  * @param  \Upload\StorageInterface  $storage The upload delegate instance
  * @throws \RuntimeException                  If file uploads are disabled in the php.ini file
  * @throws \InvalidArgumentException          If $_FILES[] does not contain key
  */
 public function __construct($key, \Upload\StorageInterface $storage)
 {
     // Check if file uploads are allowed
     if (ini_get('file_uploads') == false) {
         throw new \RuntimeException('File uploads are disabled in your PHP.ini file');
     }
     // Check if key exists
     if (isset($_FILES[$key]) === false) {
         throw new \InvalidArgumentException("Cannot find uploaded file(s) identified by key: {$key}");
     }
     // Collect file info
     if (is_array($_FILES[$key]['tmp_name']) === true) {
         foreach ($_FILES[$key]['tmp_name'] as $index => $tmpName) {
             if ($_FILES[$key]['error'][$index] !== UPLOAD_ERR_OK) {
                 $this->errors[] = sprintf('%s: %s', $_FILES[$key]['name'][$index], static::$errorCodeMessages[$_FILES[$key]['error'][$index]]);
                 continue;
             }
             $this->objects[] = \Upload\FileInfo::createFromFactory($_FILES[$key]['tmp_name'][$index], $_FILES[$key]['name'][$index]);
         }
     } else {
         if ($_FILES[$key]['error'] !== UPLOAD_ERR_OK) {
             $this->errors[] = sprintf('%s: %s', $_FILES[$key]['name'], static::$errorCodeMessages[$_FILES[$key]['error']]);
         }
         $this->objects[] = \Upload\FileInfo::createFromFactory($_FILES[$key]['tmp_name'], $_FILES[$key]['name']);
     }
     $this->storage = $storage;
 }