The following exposes the use of Flysystem's API methods throughout the component. The examples shown assume that you
have configured your component properly and its name is fs
. Examples listed:
- Writing Files
- Updating Files
- Writing or Updating Files
- Reading Files
- Checking File Existence
- Deleting Files
- Reading and Deleting Files
- Renaming Files
- Getting Files Mimetype
- Getting Files TimeStamp
- Getting Files Size
- Creating Directories
- Deleting Directories
- Managing visibility
- Listing Contents
- Listing Paths
- Listing with Specific Metadata
- Get File Info with Explicit Metadata
To write file
Yii::$app->fs->write('filename.ext', 'contents');
To write file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);
To update file
Yii::$app->fs->update('filename.ext', 'contents');
To update file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->updateStream('filename.ext', $stream);
To write or update file
Yii::$app->fs->put('filename.ext', 'contents');
To write or update file using stream contents
$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('filename.ext', $stream);
To read file
$contents = Yii::$app->fs->read('filename.ext');
To retrieve a read-stream
$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);
To check if a file exists
$exists = Yii::$app->fs->has('filename.ext');
To delete file
Yii::$app->fs->delete('filename.ext');
To read and delete file
$contents = Yii::$app->fs->readAndDelete('filename.ext');
To rename file
Yii::$app->fs->rename('filename.ext', 'newname.ext');
To get file mimetype
$mimetype = Yii::$app->fs->getMimetype('filename.ext');
To get file timestamp
$timestamp = Yii::$app->fs->getTimestamp('filename.ext');
To get file size
$timestamp = Yii::$app->fs->getSize('filename.ext');
To create directory
Yii::$app->fs->createDir('path/to/directory');
Directories are also made implicitly when writing to a deeper path
Yii::$app->fs->write('path/to/filename.ext');
To delete directory
Yii::$app->fs->deleteDir('path/to/filename.ext');
Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.
use League\Flysystem\AdapterInterface;
Yii::$app->fs->write('filename.ext', 'contents', [
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
]);
You can also change and check visibility of existing files
use League\Flysystem\AdapterInterface;
if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}
To list contents
$contents = Yii::$app->fs->listContents();
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 = Yii::$app->fs->listContents('path/to/directory', true);
To list paths
$paths = Yii::$app->fs->listPaths();
foreach ($paths as $path) {
echo $path;
}
To list with ensured presence of specific metadata
$listing = Yii::$app->fs->listWith(
['mimetype', 'size', 'timestamp'],
'optional/path/to/directory',
true
);
foreach ($listing as $object) {
echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}
To get file info with explicit metadata
$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];
Custom Software | Web & Mobile Software Development
www.2amigos.us