Home  >  Article  >  PHP Framework  >  How to use Hyperf framework for file upload

How to use Hyperf framework for file upload

WBOY
WBOYOriginal
2023-10-21 09:06:281120browse

How to use Hyperf framework for file upload

How to use the Hyperf framework for file upload requires specific code examples

Introduction:
With the development of web applications, the file upload function has become a part of many projects an essential part. Hyperf is a high-performance PHP microservice framework that provides a rich set of functions, including file upload. This article will introduce how to use the Hyperf framework for file upload and give specific code examples.

1. Install the Hyperf framework:
First, you need to install the Hyperf framework. It can be installed through the composer command:

composer create-project hyperf/hyperf-skeleton

After the installation is completed, enter the project directory and start Hyperf:

cd hyperf-skeleton
php bin/hyperf.php start

2. Write the file upload interface:
In the Hyperf framework, we can write Controller to handle the request. Create a new UploadController.php file and add the following code:

<?php

declare(strict_types=1);

namespace AppController;

use HyperfHttpServerAnnotationAutoController;
use HyperfHttpServerContractRequestInterface;
use HyperfHttpServerContractResponseInterface;
use HyperfHttpServerHttpServer;
use HyperfHttpServerRouterDispatched;
use HyperfHttpServerRouterHandler;
use HyperfHttpServerRouterRouteCollector;
use HyperfHttpServerRouterRouter;
use HyperfUtilsCodecJson;
use HyperfUtilsContext;
use PsrHttpMessageResponseInterface as Psr7ResponseInterface;

/**
 * @AutoController()
 */
class UploadController extends AbstractController
{
    /**
     * 文件上传
     */
    public function upload(RequestInterface $request): Psr7ResponseInterface
    {
        $file = $request->file('file');  // 获取上传的文件
        $uploadedPath = $file->getPath();  // 获取上传的文件的临时路径
        $filename = $file->getClientFilename();  // 获取上传的文件名
        
        // 处理上传的文件,例如保存到指定目录
        $targetPath = BASE_PATH . '/public/uploads/' . $filename;
        $file->moveTo($targetPath);
        
        return $this->success('文件上传成功');
    }
}

3. Configure routing:
In the Hyperf framework, we need to configure routing to map requests to the corresponding Controller for processing. Open the config/routes.php file and add the following code:

<?php

use HyperfHttpServerRouterRouter;

Router::addRoute(
    ['POST'],
    '/upload',
    'AppControllerUploadController@upload'
);

4. Call the file upload interface:
In the front-end page, you can upload files through a form. Configure the form's action to /upload and set the enctype to multipart/form-data. The following is a simple HTML example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传示例</title>
</head>
<body>
    <form action="/upload" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="上传">
    </form>
</body>
</html>

5. Test file upload:
After starting the Hyperf server, open the browser and enter http://localhost:9501 in the address bar , enter the file upload page. Select a file and click the upload button to complete the file upload.

Conclusion:
Through the file upload function provided by the Hyperf framework, we can easily realize the file upload requirements. This article introduces how to use the Hyperf framework for file upload and gives specific code examples. I hope it can help you implement the file upload function in your Hyperf project.

The above is the detailed content of How to use Hyperf framework for file upload. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn