WARNING: you're viewing docs for an outdated version. View the docs for the current version.

Filesystem API (V1)

The Filesystem API is the most important interface Flysystem describes when you want to use Flysystem in your application.

Write Files

$response = $filesystem->write($path, $contents [, $config]);
param description type
$path location of a file string
$contents file contents string
$config An optional configuration array array
$response success boolean bool

Write Files using a stream

$response = $filesystem->writeStream($path, $resource [, $config]);
param description type
$path location of a file string
$resource file stream resource
$config An optional configuration array array
$response success boolean bool

Update Files

$response = $filesystem->update($path, $contents [, $config]);
param description type
$path location of a file string
$contents file contents string
$config An optional configuration array array
$response success boolean bool

Update Files using a stream

$response = $filesystem->updateStream($path, $resource [, $config]);
param description type
$path location of a file string
$resource file stream resource
$config An optional configuration array array
$response success boolean bool

Write or Update Files

$response = $filesystem->put($path, $contents [, $config]);
param description type
$path location of a file string
$contents file contents string
$config An optional configuration array array
$response success boolean bool

Write or Update Files using a stream

$response = $filesystem->putStream($path, $resource [, $config]);
param description type
$path location of a file string
$resource file stream resource
$config An optional configuration array array
$response success boolean bool

Read Files

$contents = $filesystem->read($path);
param description type
$path location of a file string
$contents file contents or false on failure string|false

Read Files as a stream

$resource = $filesystem->readStream($path);
param description type
$path location of a file string
$resource file stream or false on failure resource|false

Check if a file or directory exists

$exists = $filesystem->has($path);
param description type
$path location of a file/directory string
$exists whether it exists bool

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 or Directories

$response = $filesystem->delete($path);
param description type
$path location of a file/directory string
$response success boolean bool

Read and Delete

$contents = $filesystem->readAndDelete($path);
param description type
$path location of a file string
$contents file contents or false on failure string|false

Rename Files

$response = $filesystem->rename($from, $to);
param description type
$from location of a file string
$to new location string
$response success boolean bool

When used with the Local adapter, this will rename a directory as well. This behaviour will not be present in V2.

Copy Files

$response = $filesystem->copy($from, $to);
param description type
$from location of a file string
$to new location string
$response success boolean bool

Get Mimetypes

$response = $filesystem->getMimetype($path);
param description type
$path location of a file string
$response mime-type or false on failure string|false

Get Timestamps

This function returns the last updated timestamp.

$response = $filesystem->getTimestamp($path);
param description type
$path location of a file string
$response timestamp of modification or false on failure integer|false

Get File Sizes

$response = $filesystem->getSize($path);
param description type
$path location of a file string
$response size of a file or false on failure integer|false

Create Directories

$response = $filesystem->createDir($path);
param description type
$path location of a file string
$response success boolean boolean

If needed, directories are also made implicitly when writing to a deeper path. In general creating a directory is not required in order to write to it.

Some filesystems don’t require directories at all, and for those filesystems (like AWS S3) no directories are created when writing files.


Delete Directories

Deleting directories is always done recursively.

$response = $filesystem->deleteDir($path);
param description type
$path location of a file string
$response success boolean boolean

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($path, $contents, [
    'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);

// Or simply
$filesystem->write($path, $contents, ['visibility' => 'private']);

You can also change and check visibility of existing files:

if ($filesystem->getVisibility($path) === 'private') {
    $filesystem->setVisibility($path, 'public');
}

Global visibility setting

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 = $filesystem->listContents($path, $recursive);

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);

Using streams for reads and writes

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);
}