Jyxo\Webdav\Client::mkdir PHP Method

mkdir() public method

Creates a directory.
public mkdir ( string $dir, boolean $recursive = true )
$dir string Directory path
$recursive boolean Create directories recursively?
    public function mkdir(string $dir, bool $recursive = true)
    {
        // If creating directories recursively, create the parent directory first
        $dir = trim($dir, '/');
        if ($recursive) {
            $dirs = explode('/', $dir);
        } else {
            $dirs = [$dir];
        }
        $path = '';
        foreach ($dirs as $dir) {
            $path .= rtrim($dir);
            $path = $this->getDirPath($path);
            foreach ($this->sendAllRequests($this->createAllRequests($path, self::METHOD_MKCOL)) as $response) {
                switch ($response->getStatusCode()) {
                    // The directory was created
                    case self::STATUS_201_CREATED:
                        break;
                        // The directory already exists
                    // The directory already exists
                    case self::STATUS_405_METHOD_NOT_ALLOWED:
                        break;
                        // The directory could not be created
                    // The directory could not be created
                    default:
                        throw new DirectoryNotCreatedException(sprintf('Directory %s cannot be created.', $path));
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Tests creating a directory.
  */
 public function testMkdir()
 {
     $this->assertTrue($this->client->mkdir($this->dir));
     $this->assertTrue($this->client->mkdir($this->dir . '/aaa/bbb/ccc'));
     // Already exists
     $this->assertTrue($this->client->mkdir($this->dir));
     // Could not be created
     $this->assertFalse($this->client->mkdir($this->dir . '/xxx/yyy/zzz', false));
 }