Home  >  Article  >  PHP Framework  >  ThinkPHP6 file upload and download: realize file management and storage

ThinkPHP6 file upload and download: realize file management and storage

王林
王林Original
2023-08-13 13:09:022917browse

ThinkPHP6 file upload and download: realize file management and storage

ThinkPHP6 file upload and download: realizing file management and storage

Introduction:
With the rapid development of the Internet, file upload and download have become our One of the essential functions in daily work and life. In the ThinkPHP6 framework, we can upload and download files through simple code to facilitate file management and storage. This article will introduce how to implement file upload and download functions in the ThinkPHP6 framework and provide corresponding code examples.

1. Implementation of the file upload function

  1. Create a form page for file upload
    We first create a form page for uploading files. On this page, we need to write a form for the user to select the file to upload, and then submit the file to the server through post. The code example is as follows:
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="上传" />
</form>
  1. Receive and save the uploaded file
    In the controller method that receives the file, we need to obtain the uploaded file and save it to the specified location on the server side. The code example is as follows:
public function upload()
{
    $file = request()->file('file');
    $savePath = './uploads/'; // 文件保存路径

    // 移动文件到指定位置
    $result = $file->move($savePath);
    if ($result) {
        // 文件移动成功
        echo '文件上传成功';
    } else {
        // 文件移动失败
        echo '文件上传失败';
    }
}
  1. Complete implementation of the file upload function
    Integrating the above two steps together, we can fully realize the file upload function. The code example is as follows:
public function upload()
{
    if (request()->isPost()) {
        $file = request()->file('file');
        $savePath = './uploads/'; // 文件保存路径

        // 移动文件到指定位置
        $result = $file->move($savePath);
        if ($result) {
            // 文件移动成功
            echo '文件上传成功';
        } else {
            // 文件移动失败
            echo '文件上传失败';
        }
    }

    return $this->fetch();
}

2. Implementation of the file download function

  1. Create a link for file download
    In the file download function, we need to create a file download function on the front-end page Create a download link that users can click to download the corresponding file. The code example is as follows:
<a href="/download?file=filename">下载文件</a>
  1. Implementing the file download function
    In the backend controller method, we need to get the path of the file to be downloaded and send the file through the response header Download to users. The code example is as follows:
public function download()
{
    // 获取要下载的文件路径
    $filePath = './uploads/' . input('file');

    // 文件下载
    if (file_exists($filePath)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($filePath));
        header('Content-Length: ' . filesize($filePath));
        readfile($filePath);
    } else {
        echo '文件不存在';
    }
}
  1. Complete implementation of the file download function
    By integrating the above steps, we can fully realize the file download function. The code example is as follows:
public function download()
{
    $filePath = './uploads/' . input('file');

    if (file_exists($filePath)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($filePath));
        header('Content-Length: ' . filesize($filePath));
        readfile($filePath);
    } else {
        echo '文件不存在';
    }
}

Conclusion:
Through the above code example, we can see that it is very simple to implement the file upload and download functions in the ThinkPHP6 framework. By mastering this knowledge, we can easily manage and store files and meet users' needs for file upload and download. I hope this article can be helpful to everyone in implementing file upload and download functions.

The above is the detailed content of ThinkPHP6 file upload and download: realize file management and storage. 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