PHP code example of xy_jx / thinkphp-filesystem-cloud

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

    

xy_jx / thinkphp-filesystem-cloud example snippets




namespace app\controller;

use app\BaseController;
use app\Request;
use think\facade\Filesystem;

/**
 * 公共类
 * Class PublicController
 * @package app\api\controller
 */
class PublicController extends BaseController
{
    /**
     * 图片上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function upload_image(Request $request)
    {
            $file = $request->file('file');
            if ($file) {
                //验证文件
                $this->validate($request->file(), ['file' => 'fileSize:10485760|fileMime:image/jpeg,image/png|file']);
                // 上传到阿里云oss
                $savename = Filesystem::disk('aliyun')->putFile('', $file);
                $result = [
                    'type' => $file->getMime(),
                    'extension' => $file->extension(),
                    'url' => $savename,
                    'full_url' => $type['url'] . $savename,
                ];
                return \Api::success($result);
            } else {
                return \Api::error('没有上传文件');
            }
    }
}