Newscoop\Storage::storeItem PHP Метод

storeItem() публичный Метод

Store item
public storeItem ( string $dest, string $data ) : integer
$dest string
$data string
Результат integer
    public function storeItem($dest, $data)
    {
        $path = $this->getPath($dest);
        $realpath = realpath($path);
        if ($realpath && !is_file($realpath)) {
            throw new \InvalidArgumentException($dest, self::ERROR_NOT_FILE);
        }
        $dir = dirname($path);
        if (!is_dir($dir)) {
            throw new \InvalidArgumentException(dirname($dest), self::ERROR_NOT_DIR);
        }
        return file_put_contents($path, $data);
    }

Usage Example

Пример #1
0
 /**
  * Replace item
  *
  * @param string $key
  * @param Zend_Form_Element_File $file
  * @return void
  * @throws InvalidArgumentException
  */
 public function replaceItem($key, \Zend_Form_Element_File $file)
 {
     $oldMime = current(explode(';', $this->storage->getMimeType($key)));
     $newMime = current(explode(';', $file->getMimeType()));
     if ($oldMime != $newMime && !(in_array($oldMime, self::$equivalentMimeTypes) && in_array($newMime, self::$equivalentMimeTypes))) {
         throw new \InvalidArgumentException(sprintf('You can only replace a file with a file of the same type.  The original file is of type "%s", and the file you uploaded was of type "%s".', $oldMime, $newMime));
     }
     $this->storage->storeItem($key, file_get_contents($file->getFileName()));
 }