/**
  * @covers Universal\Http\UploadedFile
  * @group upload
  * @expectedException Universal\Exception\UploadErrorException
  */
 public function testUploadError()
 {
     $tmpfile = tempnam('/tmp', 'test_');
     $this->assertNotFalse(file_put_contents($tmpfile, 'foo'));
     $filestash = array('name' => 'foo.txt', 'tmp_name' => $tmpfile, 'type' => 'text/plain', 'size' => filesize($tmpfile), 'error' => 1);
     $file = UploadedFile::createFromArray($filestash);
     $this->assertNotNull($file);
     $this->assertInstanceOf('Universal\\Http\\UploadedFile', $file);
     $file->moveTo('tests');
 }
Exemple #2
0
 /**
  * file uploading issue:
  *
  * When processing sub-actions, the file objects are shared across the
  * parent action. sometimes the column name will collide with the parent action column names.  
  * To solve this issue, we have to separate the namespace.
  *
  *   solutions:
  *     1. the file upload object should be action-wide not request-wide
  *     2. save the file upload object in the action, not in the request object.
  *     3. do not access $_FILES or request->files directly.
  *
  *   plan:
  *      1. Moved uploadedFile access methods from ActionRequest to Action itself.
  *
  */
 protected function _findUploadedFile($name, &$requireUploadMove)
 {
     // See if there is any UploadedFile object created in this action.
     $uploadedFile = $this->action->request->uploadedFile($name, 0);
     if ($uploadedFile) {
         return $uploadedFile;
     }
     // Uploaded by static path
     if ($uploadedPath = $this->action->arg($name)) {
         $fileArray = Utils::createFileArrayFromPath($uploadedPath);
         return UploadedFile::createFromArray($fileArray);
     }
     // create an uploaded file object from here
     $fileArray = $this->action->request->file($this->name);
     // if there is an upload file in $_FILES
     if ($fileArray && $fileArray['error'] == 0) {
         $requireUploadMove = true;
         return UploadedFile::createFromArray($fileArray);
     }
     return null;
 }
Exemple #3
0
 public function init(&$args)
 {
     /* how do we make sure the file is a real http upload ?
      * if we pass args to model ?
      *
      * if POST,GET file column key is set. remove it from ->args
      */
     if (!$this->putIn) {
         throw new Exception("putIn attribute is not defined.");
     }
     $file = null;
     $upload = false;
     /* if the column is defined, then use the column
      *
      * if not, check sourceField.
      * */
     if ($fileArg = $this->action->request->file($this->name)) {
         $upload = true;
         $file = $fileArg;
     } else {
         if ($this->sourceField) {
             if ($fileArg = $this->action->request->file($this->sourceField)) {
                 $file = $fileArg;
             }
         }
     }
     if (!$file) {
         return false;
     }
     // TODO: Move this to a proper place
     if ($this->putIn && !file_exists($this->putIn)) {
         mkdir($this->putIn, 0755, true);
     }
     $uploadedFile = UploadedFile::createFromArray($file);
     $newName = $uploadedFile->getOriginalFileName();
     if ($this->renameFile) {
         $newName = call_user_func($this->rename, $newName);
     }
     $targetPath = $this->putIn . DIRECTORY_SEPARATOR . $newName;
     // When sourceField enabled, we should either check saved_path or tmp_name
     if ($upload) {
         if ($savedPath = $uploadedFile->getSavedPath()) {
             $ret = $uploadedFile->move($targetPath);
         } else {
             if ($uploadedFile->isUploadedFile()) {
                 // move calls move_uploaded_file, which is only available for files uploaded from HTTP
                 $ret = $uploadedFile->move($targetPath);
             } else {
                 $uploadedFile->copy($targetPath);
             }
         }
     } else {
         if ($this->sourceField) {
             if ($savedPath = $uploadedFile->getSavedPath()) {
                 copy($savedPath, $targetPath);
             } else {
                 $uploadedFile->copy($targetPath);
             }
         } else {
             return;
         }
     }
     $args[$this->name] = $targetPath;
     $this->action->addData($this->name, $targetPath);
 }