PHP code example of enoliu / thinkphp-oss
1. Go to this page and download the library: Download enoliu/thinkphp-oss 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/ */
enoliu / thinkphp-oss example snippets
return [
// 默认磁盘
'default' => env('filesystem.driver', 'oss'),
// 磁盘列表
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
'public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/storage',
// 磁盘路径对应的外部URL路径
'url' => '/storage',
// 可见性
'visibility' => 'public',
],
// 更多的磁盘配置信息
'oss' => [
'type' => 'oss',
'accessId' => 'LT******JwHf',
'accessSecret' => 'MfSs*******cOzpP',
'bucket' => 'lo***e52',
'endPoint' => 'oss-cn-beijing.aliyuncs.com',
// 'timeout' => 3600,
// 'connectTimeout' => 10,
// 'isCName' => false,
// 'token' => '',
// 'useSSL' => false // 是否启用ssl
]
],
];
namespace app\controller;
use app\BaseController;
use think\facade\Filesystem;
class Upload extends BaseController
{
public function demo()
{
$file = $this->request->file('file');
$path1 = Filesystem::putFile('test/path', $file);
$path2 = Filesystem::putFileAs('test/path', $file, $file->getOriginalName());
$path3 = Filesystem::disk('oss')->putFile('test/path', $file); // 指定disk驱动
$url1 = Filesystem::getAdapter()->getUrl($path1);
$url2 = Filesystem::getAdapter()->getUrl($path2);
$url3 = Filesystem::disk('oss')->getAdapter()->getUrl($path3);
return compact('path1','url1', 'path2', 'url2', 'path3', 'url3');
}
}
use think\facade\Filesystem;
$config = [
'dir' => 'upload/tmp',
'expire' => 60 * 60,
'callback' => 'http://www.baidu.com',
'maxSize' => 10 * 1024 * 1024
];
array Filesystem::getAdapter()->directUpload($config);