A modern PHP WebDAV client library for Nextcloud, ownCloud, and other WebDAV servers.
- Modern PHP 8.1+ - Built with modern PHP features and type safety
- PSR-4 Autoloading - Standard PHP package structure
- Comprehensive WebDAV Support - All essential WebDAV operations
- Server Compatibility - Works with Nextcloud, ownCloud, Apache WebDAV, and more
- Easy to Use - Simple, intuitive API design
- Well Tested - Comprehensive test suite
- Production Ready - Used in production applications
Install via Composer:
composer require four-bytes/four-webdav-client-php<?php
require 'vendor/autoload.php';
use Four\WebDav\WebDavClient;
// Create client instance
$client = new WebDavClient(
baseUrl: 'https://your-nextcloud.com/remote.php/dav/files/username/',
username: 'your-username',
password: 'your-password-or-app-token'
);
// List directory contents
$response = $client->list('/');
if ($response->success) {
foreach ($response->items as $item) {
echo $item->name . ($item->isDirectory ? '/' : '') . "\n";
}
}
// Upload a file
$response = $client->uploadContent('/notes.md', '# My Notes', 'text/markdown');
if ($response->success) {
echo "File uploaded successfully!\n";
}
// Download a file
$response = $client->download('/notes.md');
if ($response->success) {
echo "File content: " . $response->content . "\n";
}public function __construct(string $baseUrl, string $username, string $password)$baseUrl- WebDAV server base URL$username- Username for authentication$password- Password or app token for authentication
List directory contents.
$response = $client->list('/documents');
foreach ($response->items as $item) {
echo "{$item->name} ({$item->formattedSize})\n";
}Download file content.
$response = $client->download('/document.txt');
if ($response->success) {
file_put_contents('local-copy.txt', $response->content);
}Upload file from local filesystem.
$response = $client->upload('/remote/file.txt', '/local/file.txt');Upload content directly.
$response = $client->uploadContent('/notes.md', '# Notes', 'text/markdown');Delete file or directory.
$response = $client->delete('/old-file.txt');Create directory.
$response = $client->createDirectory('/new-folder');Check if path exists.
if ($client->exists('/important-file.txt')) {
echo "File exists!\n";
}Search for files by name pattern.
$response = $client->searchFiles('/', '*.md');
foreach ($response->items as $item) {
echo "Found markdown file: {$item->name}\n";
}Get detailed file information.
$response = $client->getFileInfo('/document.pdf');
if ($response->success) {
$file = $response->items[0];
echo "Size: {$file->formattedSize}\n";
echo "Modified: {$file->getFormattedLastModified()}\n";
}Response object returned by all WebDAV operations.
class WebDavResponse
{
public bool $success; // Operation success status
public string $message; // Status message
public array $items; // Array of WebDavItem objects
public ?string $content; // File content (for downloads)
public ?string $contentType; // MIME type (for downloads)
}Represents a file or directory.
class WebDavItem
{
public string $name; // File/directory name
public string $path; // Full path
public bool $isDirectory; // Is directory flag
public int $size; // Size in bytes
public ?DateTime $lastModified; // Last modified date
public string $contentType; // MIME type
// Helper methods
public function isMarkdownFile(): bool;
public function getFormattedSize(): string;
public function getFormattedLastModified(): ?string;
public function toArray(): array;
}- Generate an app password: Settings → Security → App passwords
- Use URL format:
https://your-nextcloud.com/remote.php/dav/files/username/
$client = new WebDavClient(
baseUrl: 'https://cloud.example.com/remote.php/dav/files/john/',
username: 'john',
password: 'generated-app-password'
);- Use your regular credentials or app password if available
- Use URL format:
https://your-owncloud.com/remote.php/webdav/
$client = new WebDavClient(
baseUrl: 'https://owncloud.example.com/remote.php/webdav/',
username: 'john',
password: 'your-password'
);Configure Apache with mod_dav and use the WebDAV URL:
$client = new WebDavClient(
baseUrl: 'https://example.com/webdav/',
username: 'john',
password: 'your-password'
);All methods return a WebDavResponse object with success status:
$response = $client->upload('/file.txt', '/local/file.txt');
if (!$response->success) {
echo "Upload failed: " . $response->message . "\n";
exit(1);
}
echo "Upload successful!\n";For boolean methods like exists(), exceptions are caught internally:
// This will return false if the file doesn't exist or there's an error
$exists = $client->exists('/some-file.txt');Run the test suite:
composer testRun with coverage:
composer test-coverageStatic analysis:
composer phpstanCode style check:
composer cs-check
composer cs-fix- PHP 8.1 or higher
- Guzzle HTTP client library
- SimpleXML extension
- LibXML extension
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Issues: GitHub Issues
- Documentation: This README and inline documentation
- Email: info@4bytes.de