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

How to use Hyperf framework for file downloading

WBOY
WBOYOriginal
2023-10-21 08:23:141545browse

How to use Hyperf framework for file downloading

How to use the Hyperf framework for file download

Introduction:
When using the Hyperf framework to develop web applications, file downloading is a common requirement. This article will introduce how to use the Hyperf framework for file downloading, including specific code examples.

1. Preparation
Before you start, make sure you have installed the Hyperf framework and successfully created a Hyperf application.

2. Create a file download controller
First, we need to create a controller to handle file download requests. Open the terminal, enter the root directory of the Hyperf project, and execute the following command to generate a download controller:

php bin/hyperf.php generate:controller DownloadController

The generated controller file is located in the app/Controller directory. Open the file and modify it. index()The method is as follows:

<?php

declare(strict_types=1);

namespace AppController;

use HyperfHttpServerAnnotationController;
use HyperfHttpServerAnnotationRequestMapping;
use HyperfHttpMessageStreamSwooleStream;
use HyperfUtilsCodecEncode;
use HyperfUtilsCodecJson;
use HyperfUtilsCodecXml;

/**
 * @Controller
 */
class DownloadController
{
    /**
     * 下载文件
     * @RequestMapping(path="/download", methods="get")
     */
    public function index()
    {
        // 文件路径
        $file = '/path/to/file'; // 替换成实际文件路径

        // 文件名
        $filename = 'filename.ext'; // 替换成实际文件名

        // 设置响应头
        return response()
            ->header('Content-Type', 'application/octet-stream')
            ->header('Content-Disposition', 'attachment; filename=' . $filename)
            ->stream(new SwooleStream(file_get_contents($file)));
    }
}

Here we use the response() function to create a response instance and set the response through the header() method Header, stream() method is used to send file content. You need to replace the $file variable with the actual file path and the $filename variable with the actual file name.

3. Configure routing
Next, we need to configure a route to access the download controller. Open the config/routes.php file and add the following code:

<?php
//...
use AppControllerDownloadController;
//...
Router::get('/download', [DownloadController::class, 'index']);
//...

This will configure a GET request route. When accessing /download, it will Execute the index method of DownloadController.

4. Start the server
Start the Hyperf server to listen for HTTP requests. Enter the root directory of the Hyperf project in the terminal and execute the following command:

php bin/hyperf.php start

After the server is started, you can download the file by accessing http://localhost:9501/download.

Conclusion:
This article introduces how to use the Hyperf framework for file downloading, including creating a download controller, configuring routing and starting the server. Through the sample code in this article, you can easily implement the file download function in your Hyperf project. Hope this article is helpful to you!

The above is the detailed content of How to use Hyperf framework for file downloading. 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