?? ??? ?? Hyperf ?????? ???? ??
Oct 25, 2023 pm 12:34 PM?? ??? ?? Hyperf ?????? ????? ?? ?? ??? ?????.
Hyperf? Swoole ??? ???? ??? ??? PHP ?????? ???, ??? ??, AOP, ???? ? ??? ??? ?? ??? ??? ??? ????. . ???, ???? ?? ??? ? ?????? ? ???????? ???? ? ?????.
?? ??????? ??? ???? ???? ?? ??? ????. Hyperf ?????? ?? ?? ??? ????? ? ??? ?? ? ?? ??? ?? ??? ??? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ???? ?? ??? ?????.
1. ??? ??
?? Hyperf ????? ??? ???? ???? ???. ???? ?? ???? ?? ????? ??? ? ?? ??? ?????.
composer require hyperf/filesystem
2. ?? ??? ??
Hyperf ???????? hyperf/filesystem ?? ??? ? ?? ??? ?????. ?? ?? ???? ???? ???. <code>config/autoload/filesystem.php
??? ?? ??? ?????: hyperf/filesystem
組件來實現(xiàn)文件存儲。首先,我們需要對文件系統(tǒng)進行配置。在config/autoload/filesystem.php
文件中,添加如下代碼:
return [ 'default' => 'local', 'disks' => [ // 本地文件系統(tǒng) 'local' => [ 'driver' => 'local', 'root' => 'runtime/files', ], // 其他文件系統(tǒng)配置... ], ];
在上述配置中,我們使用driver
參數(shù)指定了文件系統(tǒng)的類型,這里我們選擇了local
,表示使用本地文件系統(tǒng)。root
參數(shù)指定了存儲文件的根目錄,在這里我們選擇了runtime/files
,你可以根據(jù)實際情況自行修改。
三、使用文件系統(tǒng)
配置完成后,我們就可以使用文件系統(tǒng)進行文件存儲了。在Hyperf框架中,我們可以通過依賴注入來使用文件系統(tǒng)。首先,在需要使用文件系統(tǒng)的類中,添加如下代碼:
use HyperfFilesystemFilesystemFactory;
然后,在類的構(gòu)造函數(shù)中注入文件系統(tǒng):
protected $filesystem; public function __construct(FilesystemFactory $filesystemFactory) { $this->filesystem = $filesystemFactory->get('local'); }
以上代碼中,我們通過FilesystemFactory
類獲取了一個名為local
的文件系統(tǒng)實例。
四、文件存儲
在實際應(yīng)用中,我們通常需要將用戶上傳的文件存儲到文件系統(tǒng)中。下面是一個示例,演示了如何使用Hyperf框架將文件存儲到本地文件系統(tǒng)中:
use HyperfHttpServerAnnotationAutoController; use HyperfHttpServerAnnotationMiddleware; use HyperfHttpServerContractRequestInterface; use HyperfHttpServerContractResponseInterface; use HyperfUtilsContext; use HyperfFilesystemFilesystemFactory; /** * Class FileController * @package AppController * @AutoController() * @Middleware(JwtAuthMiddleware::class) */ class FileController extends AbstractController { protected $filesystem; public function __construct(FilesystemFactory $filesystemFactory) { $this->filesystem = $filesystemFactory->get('local'); } public function upload(RequestInterface $request, ResponseInterface $response) { // 獲取上傳的文件對象 $file = $request->file('file'); // 判斷文件是否上傳成功 if ($file->isValid()) { // 獲取文件名 $fileName = $file->getClientOriginalName(); // 生成文件保存路徑 $filePath = 'upload/' . date('Y/m/d/') . uniqid() . '_' . $fileName; // 使用文件系統(tǒng)保存文件 $this->filesystem->put($filePath, file_get_contents($file->getRealPath())); // 返回文件路徑等信息給前端 return ['code' => 0, 'msg' => '上傳成功', 'data' => ['path' => $filePath]]; } else { // 文件上傳失敗 return ['code' => 1, 'msg' => '文件上傳失敗']; } } // 其他文件操作... }
以上代碼中,upload
方法接收一個RequestInterface
對象和一個ResponseInterface
對象,通過$request->file('file')
方法獲取到上傳的文件對象。然后,我們可以通過文件對象的方法獲取文件名、文件大小等信息,并使用文件系統(tǒng)$this->filesystem
的put
rrreee
driver
????? ???? ?? ??? ??? ?????. . ??? local
? ????? ?? ?? ?? ???? ????? ?????. root
????? ??? ???? ?? ????? ?????. ???? ?? ??? ?? runtime/files
? ?????. 3. ?? ??? ????? ???? ?? ???? ?? ???? ??? ? ????. Hyperf ???????? ??? ??? ?? ?? ???? ??? ? ????. ?? ?? ???? ???? ?? ???? ?? ??? ?????. rrreee
?? ?? ??? ???? ?? ???? ?????. ??rrreee??? ?????FilesystemFactory? ?? ?????.
??? local
??? ?? ??? ???????. ????4. ?? ???????? ?? ??????? ????? ???? ???? ??? ?? ???? ???? ???. ??? Hyperf ?????? ???? ?? ?? ???? ??? ???? ??? ???? ????. ??rrreee??? ???? upload
???? RequestInterface
??? ????. ? ResponseInterface ??? ?? $request->file('file')
???? ?? ???? ?? ??? ?????. ?? ?? ?? ?? ???? ?? ?? ??, ?? ?? ? ?? ??? ?? ? ???, ?? ??? $this->filesystem
? put
???? ???? ??? ???? ??? ?????. ?????? ?? ??? ?? Hyperf ?????? ???? ??? ???????. ?? ??? ?? ?? ?? ? ??? ??? ? ????. ??????????? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ???? ?? ??? ?????. Hyperf ?????? ?? ??? ?? ??? ???? ?? ???, ????, ??? ?? ???? ??? ?? ??? ? ????. ? ?? Hyperf ?????? ???? ???? ? ??? ??? ????. ??? ??? ???? ???? ????? ???????. ??? ??? ?? ??? ?? Hyperf ?????? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











??? ? ?? ??? ?? Hyperf ?????? ???? ?? ??: ?? ???? ?????? ???? ??? ? ??? ???? ?? ??? ?????. ?????? ??? ??? ??? ???? ??? ??? ????? ?? ??? ? ?? ??? Hyperf ?????? ???? ?? ?? ???????. ? ????? ??? ? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?????. 1. ??? ??? ???? XMLHttpReques? ?? ?????? ???? JavaScript? ?????.

?? ??? ?? Hyperf ?????? ????? ???? ?? ??? ?????. Hyperf? Swoole ??? ???? ??? ??? PHP ??????, ???, ??? ??, AOP, ????, ??? ?? ?? ??? ??? ??? ????. ???, ???? ?? ??? ? ?????? ? ???????? ???? ? ?????. ?? ??????? ??? ???? ???? ?? ??? ????. Hyperf ?????? ?? ?? ??? ????? ? ??? ?? ? ?? ??? ?? ??? ??? ?????. ? ????? ?? ??? ?????.

?? ??? ?? Hyperf ?????? ????? ?? ?? ??? ?????. ??: ????? ?? ?????? ??? ??? ??? ???? ???? ???? ???. ??? PHP ?? ?????? Hyperf ?????? ???? ?? ??? ???? ? ??? ?? ??? ??? ??? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?? ?? ?????. 1. ?? ?? ?? ?? Hyperf ?????? ? ?? ???? ??? ?????.

?? ??? ?? Hyperf ?????? ???? ?? ??: Hyrpf? ??? ?? ??? ??? ?? PHP ?? ??? ???, ?? ??? ??? ????????. ?? ??? ?? ????? ???? ?????. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ?? ?? ??? ?????. 1. Hyperf ????? ?? ?? Hyperf ?????? ???? ???. Composer? ?? ??? ? ???, ??? ??? ?? ?? ??? ?????.

JWT ??? ?? Hyperf ?????? ???? ?? ??: Hyperf? Swoole ??? ??? ??? ?????? ??? ??? ??? ???? ?????. JWT(JSONWebToken)? ??? ???? ???? ?? ??? ?????. ? ????? Hyperf ??????? JWT ??? ???? ??? ???? ???? ?? ??? ?????. 1. ??? ??? ?? ?? hyperf/jwt ? lcobucci/jw? ???? ???.

?? ???? ?? Hyperf ?????? ???? ?? ??: ?? ???? ?? ??? ??? ???? ??? ? ?? ????? ??? ??? ?????. Hyperf? Swoole ??? ???? ??? ??? ?????? ?? ???? ???? ?? ??? ??? ?? ? ?? ??? ?????. ? ????? ?? ???? ?? Hyperf ?????? ???? ??? ???? ? ?? ?? ?? ??? ?????. 1. ?? ?? ?? ???? ???? ?? Hyperf ??????

Golang ??????? ???? ???? ?? ?? ? ?? Golang ??????? ???? ??? ????? ????? ???? ?? ???? ????? ? ??? ?? ?? ??? ?????. Golang(Go ??) ??? ???? ?? ???? ??? ?? ?? ??? ??? ?? ??? ???? ???? ??? ??? ??? ? ????. ? ????? Golang ?????? ??? ?? ??? ?? ??? ??? ???? ???? ?? ??? ?????. ???? ????? ?????

?? ??? ?? Hyperf ?????? ???? ?? ??: ?? ????? ???? ?? ???? ?? ???? ?? ??? ?? ?????. ?? ??? ??? ?????? ???? ???? ??? ???? ??? ????? ? ??? ???. ? ????? ?? ??? ?? Hyperf ?????? ???? ??? ???? ???? ?? ??? ?????. 1. ????? ????? ??? ??? ??? ??? ??? ??? ? ???? ????? ??? ? ??? ??? ??? ???? ???? ???? ?? ?????. ??
