PHP code example of selden1992 / think-flysystem

1. Go to this page and download the library: Download selden1992/think-flysystem 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/ */

    

selden1992 / think-flysystem example snippets




/**
 * 演示配置
 */
return [

    // 默认驱动
    'default'=>'local',
    // 本地驱动
    'local'=>[
        'adapter_class'=>\Think\flysystem\adapter\Local::class,
        // 跟目录
        'root'=>'./files/',
        // 权限参数
        'permissions'=>[
            'file' => [
                'public' => 0744,
                'private' => 0700,
            ],
            'dir' => [
                'public' => 0755,
                'private' => 0700,
            ]
        ],
        // 目录别名
        'alias'=>[
            'image'=>'image/user/',
        ],
    ],
    // ftp 扩展
    'ftp'=>[
        'adapter_class'=>\Think\flysystem\adapter\Ftp::class,
        // 权限参数
        'permissions'=>[
            'host' => 'ftp.example.com',
            'username' => 'username',
            'password' => 'password',

            /** optional config settings */
            'port' => 21,
            'root' => '/path/to/root',
            'passive' => true,
            'ssl' => true,
            'timeout' => 30,
        ],
        // 目录别名
        'alias'=>[
            'image'=>'image/user/',
        ],
    ],
    // sftp 扩展
    'sftp'=>[
        'adapter_class'=>\Think\flysystem\adapter\Sftp::class,
        // 权限参数
        'permissions'=>[
            'host' => 'example.com',
            'port' => 21,
            'username' => 'username',
            'password' => 'password',
            'privateKey' => 'path/to/or/contents/of/privatekey',
            'root' => '/path/to/root',
            'timeout' => 10,
        ],
        // 目录别名
        'alias'=>[
            'image'=>'image/user/',
        ],
    ],
];


putenv(‘TMPDIR=/Users/cyz/web/test’);



namespace app\index\controller;

use Think\flysystem\Files;

class Index
{
    public function index()
    {
        // 普通写入
        Files::put('log/test.log',time());
        // 目录别名写入
        Files::alias('log_alias')->put('test2.log',time());
        // 指定驱动
        Files::disk('local')->put('test3.log',time());

        // 普通读取
        echo Files::read('log/test.log');
        // 目录别名读取
        echo Files::alias('log_alias')->read('test2.log');
        
        // 复制
        Files::copy('log/test.log','log/image1.log');
        // 第二参数路径使用别名
        Files::copy('log/test.log',['alias'=>'local','path'=>'image1.log']);
        
        // 获取别名真实路径
        echo Files::getAliasPath('image');  
                
        /**
         * ftp 操作
         */
        Files::disk('ftp')->put('log/test4.log',time());
        echo Files::disk('ftp')->alias('log_alias')->read('test4.log');

        /**
         * sftp 操作
         * 需要按 composer 

Files::write('path/to/file.txt', 'contents');

Files::update('path/to/file.txt', 'new contents');

Files::put('path/to/file.txt', 'contents');

$contents = Files::read('path/to/file.txt');

$exists = Files::has('path/to/file.txt');

Files::delete('path/to/file.txt');

$contents = Files::readAndDelete('path/to/file.txt');

Files::rename('filename.txt', 'newname.txt');

Files::copy('filename.txt', 'duplicate.txt');

$mimetype = Files::getMimetype('path/to/file.txt');

$timestamp = Files::getTimestamp('path/to/file.txt');

$size = Files::getSize('path/to/file.txt');

Files::createDir('path/to/nested/directory');

Files::write('path/to/file.txt', 'contents');

Files::deleteDir('path/to/directory');