PHP code example of hulang / think-filesystem
1. Go to this page and download the library: Download hulang/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/ */
hulang / think-filesystem example snippets
'aliyun' => [
'type' => 'aliyun',
'access_id' => '******',
'access_secret' => '******',
'bucket' => 'bucket',
'endpoint' => 'oss-cn-hongkong.aliyuncs.com',
'isCName' => true,
'cdnUrl' => '',
'prefix' => '',
'options' => [
'endpoint' => '',
'bucket_endpoint' => '',
],
],
'qiniu' => [
'type' => 'qiniu',
'access_key' => '******',
'secret_key' => '******',
'bucket' => 'bucket',
'domain' => 'https://youcdn.domain.com',
],
'qcloud' => [
'type' => 'qcloud',
'region' => '***', //bucket 所属区域 英文
'app_id' => '***', // 域名中数字部分
'secret_id' => '***',
'secret_key' => '***',
'bucket' => '***',
'timeout' => 60,
'connect_timeout' => 60,
'cdn' => '您的 CDN 域名',
'scheme' => 'https',
'read_from_cdn' => false,
]
'obs'=>[
'type' =>'obs',
'root' => '',
'key' => env('OBS_KEY'),
'secret' => env('OBS_SECRET'),
'bucket' => env('OBS_BUCKET'),
'endpoint' => env('OBS_ENDPOINT'),
'is_cname' => env('OBS_IS_CNAME', false), //true or false...
'security_token' => env('OBS_SECURITY_TOKEN'),//true or false...
],
's3'=>[
'type' =>'s3',
'credentials' => [
'key' => 'S3_KEY',
'secret' => 'S3_SECRET',
],
'region' => 'S3_REGION',
'version' => 'latest',
'bucket_endpoint' => false,
'use_path_style_endpoint' => false,
'endpoint' => 'S3_ENDPOINT',
'bucket' => 'S3_BUCKET',
],
'google'=>[
'type' =>'google',
'project_id' => 'GOOGLE_PROJECT_ID',//your-project-id
'bucket' => 'GOOGLE_BUCKET', //your-bucket-name
'prefix' => '', //optional-prefix
],
'ftp'=[
'type' =>'ftp',
'host' => 'example.com',
'username' => 'username',
'password' => 'password',
// 可选的 FTP 设置
// 'port' => 21,
// 'root' => '',
// 如果提示:php_connect_nonb之类的错误,请尝试设置为false
'passive' => false,
// 'ssl' => true,
// 'timeout' => 30,
// 'url'=>''
],
'sftp'=>[
'type' =>'sftp',
'host' => 'example.com',
// 基于基础的身份验证设置...
'username' => 'username',
'password' => 'password',
// 使用加密密码进行基于 SSH 密钥的身份验证的设置...
'privateKey' => null,
'passphrase' => null,
// 可选的 SFTP 设置
'port' => 22,
'root' => '/path/to/root',
'url' => '/path/to/root',
'timeout' => 10
],
'web' => [
'type' => 'web',
// 自定义配置
'secret_id' => 'd73d84e14cf8cd0c53855c2bea35e95ecf00689b',
'secret_key' => '5003aec07e6a2591ef2b9ed5b6e31555ab4bea33',
'bucket' => 'bucket',
// 请求接口
'domain' => 'https://img.你自己的域名.cn/上传接口',
// 访问URL
'url' => 'https://img.你自己的域名.cn/',
],
$file = $this->request->file('image');
try {
validate(
[
'image' => [
// 限制文件大小(单位b),这里限制为4M
'fileSize' => 10 * 1024 * 1000,
// 限制文件后缀,多个后缀以英文逗号分割
'fileExt' => 'gif,jpg,png,jpeg'
]
]
)->check(['image' => $file]);
$path = \hulang\filesystem\facade\Filesystem::disk('public')->putFile('test', $file);
$url = \hulang\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),
});
}
}