Imbo\Storage\Filesystem::store PHP Method

store() public method

public store ( $user, $imageIdentifier, $imageData )
    public function store($user, $imageIdentifier, $imageData)
    {
        if (!is_writable($this->params['dataDir'])) {
            throw new StorageException('Could not store image', 500);
        }
        if ($this->imageExists($user, $imageIdentifier)) {
            return touch($this->getImagePath($user, $imageIdentifier));
        }
        $imageDir = $this->getImagePath($user, $imageIdentifier, false);
        $oldUmask = umask(0);
        if (!is_dir($imageDir)) {
            mkdir($imageDir, 0775, true);
        }
        umask($oldUmask);
        $imagePath = $imageDir . '/' . $imageIdentifier;
        $bytesWritten = file_put_contents($imagePath, $imageData);
        // if write failed or 0 bytes were written (0 byte input == fail), or we wrote less than expected
        if (!$bytesWritten || $bytesWritten < strlen($imageData)) {
            throw new StorageException('Failed writing file (disk full? zero bytes input?) to disk: ' . $imagePath, 507);
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * @covers Imbo\Storage\Filesystem::store
  */
 public function testStore()
 {
     $imageData = file_get_contents(FIXTURES_DIR . '/image.png');
     $baseDir = 'someDir';
     // Create the virtual directory
     vfsStream::setup($baseDir);
     $driver = new Filesystem(['dataDir' => vfsStream::url($baseDir)]);
     $this->assertTrue($driver->store($this->user, $this->imageIdentifier, $imageData));
 }