layout: default permalink: /api/
Write Files
$filesystem->write('path/to/file.txt', 'contents');
Update Files
$filesystem->update('path/to/file.txt', 'new contents');
Write or Update Files
$filesystem->put('path/to/file.txt', 'contents');
Read Files
$contents = $filesystem->read('path/to/file.txt');
Check if a file exists
$exists = $filesystem->has('path/to/file.txt');
NOTE: This only has consistent behaviour for files, not directories. Directories are less important in Flysystem, they're created implicitly and often ignored because not every adapter (filesystem type) supports directories.
Delete Files
$filesystem->delete('path/to/file.txt');
Read and Delete
$contents = $filesystem->readAndDelete('path/to/file.txt');
Rename Files
$filesystem->rename('filename.txt', 'newname.txt');
Copy Files
$filesystem->copy('filename.txt', 'duplicate.txt');
Get Mimetypes
$mimetype = $filesystem->getMimetype('path/to/file.txt');
Get Timestamps
$timestamp = $filesystem->getTimestamp('path/to/file.txt');
Get File Sizes
$size = $filesystem->getSize('path/to/file.txt');
Create Directories
$filesystem->createDir('path/to/nested/directory');
Directories are also made implicitly when writing to a deeper path
$filesystem->write('path/to/file.txt', 'contents');
Delete Directories
$filesystem->deleteDir('path/to/directory');
The above method will delete directories recursively
NOTE: All paths used by Flysystem API are relative to the adapter root directory.
Manage Visibility
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\AdapterInterface;
$filesystem->write('db.backup', $backup, [
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);
// or simply
$filesystem->write('db.backup', $backup, ['visibility' => 'private']);
You can also change and check visibility of existing files
if ($filesystem->getVisibility('secret.txt') === 'private') {
$filesystem->setVisibility('secret.txt', 'public');
}
You can set the visibility as a default, which prevents you from setting it all over the place.
$filesystem = new League\Flysystem\Filesystem($adapter, [
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);
List Contents
$contents = $filemanager->listContents();
The result of a contents listing is a collection of arrays containing all the metadata the file manager knows at that time. By default you'll receive path info and file type. Additional info could be supplied by default depending on the adapter used.
Example:
foreach ($contents as $object) {
echo $object['basename'].' is located at'.$object['path'].' and is a '.$object['type'];
}
By default Flysystem lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results
$contents = $filesystem->listContents('some/dir', true);
List paths
$filesystem->addPlugin(new ListPaths());
$paths = $filesystem->listPaths();
foreach ($paths as $path) {
echo $path;
}
List with ensured presence of specific metadata
$listing = $filesystem->listWith(['mimetype', 'size', 'timestamp'], 'optional/path/to/dir', true);
foreach ($listing as $object) {
echo $object['path'].' has mimetype: '.$object['mimetype'];
}
Get file into with explicit metadata
$info = $filesystem->getWithMetadata('path/to/file.txt', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];
NOTE: This requires the League\Flysystem\Plugin\GetWithMetadata
plugin.
Some SDK's close streams after consuming them, therefore, before calling fclose on the resource, check if it's still valid using is_resource
.
$stream = fopen('/path/to/database.backup', 'r+');
$filesystem->writeStream('backups/'.strftime('%G-%m-%d').'.backup', $stream);
// Using write you can also directly set the visibility
$filesystem->writeStream('backups/'.strftime('%G-%m-%d').'.backup', $stream, [
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);
if (is_resource($stream)) {
fclose($stream);
}
// Or update a file with stream contents
$filesystem->updateStream('backups/'.strftime('%G-%m-%d').'.backup', $stream);
// Retrieve a read-stream
$stream = $filesystem->readStream('something/is/here.ext');
$contents = stream_get_contents($stream);
fclose($stream);
// Create or overwrite using a stream.
$putStream = tmpfile();
fwrite($putStream, $contents);
rewind($putStream);
$filesystem->putStream('somewhere/here.txt', $putStream);
if (is_resource($putStream)) {
fclose($putStream);
}