Newscoop\Storage::moveItem PHP Method

moveItem() public method

Move item
public moveItem ( string $src, string $dest ) : void
$src string
$dest string
return void
    public function moveItem($src, $dest)
    {
        $srcPath = $this->getPath($src, TRUE);
        if (!$srcPath || !is_file($srcPath)) {
            // src not found or !file
            throw new \InvalidArgumentException($src);
        }
        $destPath = $this->getPath($dest, TRUE);
        if (!$destPath || !is_dir($destPath)) {
            // dest not found or !dir
            throw new \InvalidArgumentException($dest);
        }
        $name = basename($srcPath);
        $destName = "{$destPath}/{$name}";
        if (realpath($destName)) {
            // dest/name exists
            throw new \InvalidArgumentException("{$dest}/{$name}");
        }
        rename($srcPath, $destName);
        $this->replace($src, "{$dest}/{$name}");
    }

Usage Example

示例#1
0
 /**
  * Move item
  *
  * @param string $src
  * @param string $dest
  * @return void
  * @throws InvalidArgumentException
  */
 public function moveItem($src, $dest)
 {
     try {
         $name = basename($src);
         $this->storage->moveItem($src, $dest);
         $this->repository->updateKey($src, "{$dest}/{$name}");
     } catch (\InvalidArgumentException $e) {
         throw new \InvalidArgumentException(sprintf("Can't move file %s.", $name), $e->getCode(), $e);
     }
 }