PHP code example of vietrue / think-filesystem

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

    

vietrue / think-filesystem example snippets


'aliyun' => [
    'type'         => 'aliyun',
    'accessId'     => '******',
    'accessSecret' => '******',
    'endpoint'     => 'oss-cn-shenzhen.aliyuncs.com',
    'bucket'       => '******',
    'domain'       => '',
    'throw'        => true,
    'isCName'       => true,
    'cdnUrl'=>'',
    'prefix'        => '',
    'options'       => [
          'endpoint'        => '',
          'bucket_endpoint' => '',
     ],
],
'qiniu'  => [
    'type'      => 'qiniu',
    'accessKey' => '******',
    'secretKey' => '******',
    'bucket'    => '******',
    'domain'    => '******',
    'throw'     => true,
],

$file = $this->request->file( 'image' );
      try {
            validate(
                ['image' => [
                        // 限制文件大小(单位b),这里限制为4M
                        'fileSize' => 10 * 1024 * 1000,
                        // 限制文件后缀,多个后缀以英文逗号分割
                        'fileExt'  => 'gif,jpg,png,jpeg'
                    ]
                ])->check( ['image' => $file] );

            $path     = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->putFile( 'test',$file);
            $url      = \yzh52521\filesystem\facade\Filesystem::disk( 'public' )->url( $path );
            return json( ['path' => $path,'url'  => $url] );
      } catch ( \think\exception\ValidateException $e ) {
            echo $e->getMessage();
     }

$contents = Filesystem::get('file.jpg');

if (Filesystem::disk('local')->exists('file.jpg')) {
    // ...
}

if (Filesystem::disk('local')->missing('file.jpg')) {
    // ...
}

return Filesystem::download('file.jpg');

return Filesystem::download('file.jpg', $name, $headers);

$url = Filesystem::url('file.jpg');

$size = Filesystem::size('file.jpg');

$time = Filesystem::lastModified('file.jpg');

$mime = Filesystem::mimeType('file.jpg')

$path = Filesystem::path('file.jpg');

Filesystem::put('file.jpg', $contents);

Filesystem::put('file.jpg', $resource);

if (! Filesystem::put('file.jpg', $contents)) {
    // 该文件无法写入磁盘...
}

'public' => [
    'type' => 'local',
    // ...
    'throw' => true,
],

Filesystem::prepend('file.log', 'Prepended Text');

Filesystem::append('file.log', 'Appended Text');

Filesystem::copy('old/file.jpg', 'new/file.jpg');

Filesystem::move('old/file.jpg', 'new/file.jpg');

use think\File;

// 为文件名自动生成一个唯一的 ID...
$path = Filesystem::putFile('photos', new File('/path/to/photo'));

// 手动指定一个文件名...
$path = Filesystem::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');

Filesystem::putFile('photos', new File('/path/to/photo'), 'public');

Filesystem::delete('file.jpg');

Filesystem::delete(['file.jpg', 'file2.jpg']);

Filesystem::disk('s3')->delete('path/file.jpg');

$files = Filesystem::files($directory);
$files = Filesystem::allFiles($directory);

$directories = Filesystem::directories($directory);
$directories = Filesystem::allDirectories($directory);

Filesystem::makeDirectory($directory);

Filesystem::deleteDirectory($directory);

use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;

class AppService extends Service
{
    public function boot()
    {
        Filesystem::extend('dropbox', function (App $app, array $config) {
            $adapter = new DropboxAdapter(new DropboxClient(
                $config['authorization_token']
            ));
           return new Filesystem($adapter, $config),
        });
   }
}