PHP code example of libratechie / flysystem-aliyun

1. Go to this page and download the library: Download libratechie/flysystem-aliyun library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

libratechie / flysystem-aliyun example snippets


use League\Flysystem\Filesystem;
use Libratechie\Flysystem\Aliyun\AliyunAdapter;

$accessKeyId = 'your-access-id';
$accessKeySecret = 'your-access-key';
$bucket = 'your-bucket-name';
$endpoint = 'your-endpoint'; // e.g., oss-cn-guangzhou.aliyuncs.com

$adapter = new AliyunAdapter($accessKeyId, $accessKeySecret, $endpoint, $bucket);

$config = [
    'public_url' => 'your-public-url',
    // e.g., https://your-bucket-name.oss-cn-guangzhou.aliyuncs.com
];
$filesystem = new Filesystem($adapter, $config);

// Checking if a File Exists
$filesystem->fileExists('path/to/file.txt');

// Checking if a Directory Exists
$filesystem->directoryExists('path/to');

// Checking if a File or Folder Exists
$filesystem->directoryExists('path/to/file.txt');

// Writing a File
$filesystem->write('path/to/file.txt', 'contents');

// Write Use writeStream
$stream = fopen('local/path/to/file.txt', 'r+');
$filesystem->writeStream('path/to/file.txt', $stream);

// Create a directory
$filesystem->createDirectory('path/to/directory');

// Move a file
$filesystem->rename('path/to/file.txt', 'new/path/to/file.txt');

// Copy a file
$filesystem->copy('path/to/file.txt', 'new/path/to/file.txt');

// Set the visibility of a file to 'public'
$filesystem->setVisibility('path/to/file.txt', 'public');

// Get the visibility of a file
// default: Inherits Bucket permissions. The read/write permissions of individual files are determined by the Bucket's read/write permissions.
// private: Private. All access operations to the file 

// Listing Contents of a Directory
$contents = $filesystem->listContents('path/to/directory', true);
foreach ($contents as $object) {
    echo $object['type'] . ': ' . $object['path'] . PHP_EOL;
}

// Reading a File
$contents = $filesystem->read('path/to/file.txt');

// Get the last modified time of a file
$lastModified = $filesystem->lastModified('path/to/file.txt');

// Get the file size
$fileSize = $filesystem->fileSize('path/to/file.txt');

// Get the mime type of file
$mimeType = $filesystem->mimeType('path/to/file.txt');

// Assuming 'public_url' is configured in the $config array
// Generate a public URL for a file
$publicUrl = $this->filesystem->publicUrl($path);

// Sign URL with specified expiration time in seconds and HTTP method.
// The signed URL could be used to access the object directly.
$expiresAt = new DateTimeImmutable('+1 hour');
$privateUrl = $this->filesystem->temporaryUrl($path, $expiresAt);

// Deleting a File
$filesystem->delete('path/to/file.txt');

// Deleting a Directory
$filesystem->deleteDirectory('path/to');