PHP code example of libratechie / think-filesystem

1. Go to this page and download the library: Download libratechie/think-filesystem 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 / think-filesystem example snippets


use \Libratechie\Think\Filesystem;
$disk = Filesystem::disk('public');

$disk = app('filesystem')::disk('qiniu');

return [
    'disks'   => [
        //...
        'local'  => [
            'type' => 'local',
            'root' => app()->getRuntimePath() . 'storage',
        ],
        'public' => [
            'type'       => 'local',
            'root'       => app()->getRootPath() . 'public/storage',
            'url'        => '/storage',
            'visibility' => 'public',
        ],
        //...
    ],
];

return [
    'disks'   => [
        //...
        'aliyun' => [
            'type'            => 'aliyun',
            'accessKeyId'     => '<aliyun access id>',
            'accessKeySecret' => '<aliyun access secret>',
            'bucket'          => '<bucket name>',
            'endpoint'        => '<endpoint address>',
            // 'domain'       => 'bucket.oss-cn-guangzhou.aliyuncs.com',
            // or with protocol: https://bucket.oss-cn-guangzhou.aliyuncs.com
        ]
        //...
    ],
];

return [
    'disks'   => [
        //...
        'qiniu'  => [
            'type'      => 'qiniu',
            'accessKey' => '<qiniu access key>',
            'secretKey' => '<qiniu secret key>',
            'bucket'    => '<bucket name>',
            'domain'    => 'xxxxx.hn-bkt.clouddn.com',
            // or with protocol: https://xxxxx.hn-bkt.clouddn.com
        ],
        //...
    ],
];

// Write files
$folderPath = '/path/to';
$file = request()->file('file');

// Use the default naming convention to write the file
$fileName = $disk->putFile($folderPath, $file);
// $fileName: /path/to/20240725/2697c763c84fe48d0166d0cd37181e19.jpg

// Use SHA-256 hash as the file name
$fileName = $disk->putFile($folderPath, $file, 'sha256');
// $fileName: /path/to/55/fd6b615cb02ce73c8e708ac62c9fe9c0cdd92d9161c57186e592d2b672e6e3.jpg

// Use a custom callback function to generate the file name
$fileName = $disk->putFile($folderPath, $file, function ($fileHash) {
    return 'custom' . DIRECTORY_SEPARATOR . md5($fileHash->getPathname());
});
// $fileName: /path/to/custom/db8fbf2c977c3fae6276521f788d5183.jpg

// Save the file with a specified file name
$fileName = $disk->putFileAs($folderPath, $file, 'custom.txt');

// Check whether a file exists.
$exists = $disk->fileExists('/path/to/custom.txt');

// Get file access path.
$exists = $disk->url('/path/to/custom.txt');

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

// Get file mimeType.
$size = $disk->mimeType('/path/to/custom.txt');

// Copy files
$disk->copy('/path/to/file.txt', '/path/to/copy_file.txt');

// Move files
$disk->move('/path/to/custom.txt', '/path/to/moved_file.txt');

// Delete files
$disk->delete('/path/to/file.txt');