Home > PHP Framework > ThinkPHP > How to perform multiple file upload operations in ThinkPHP6?

How to perform multiple file upload operations in ThinkPHP6?

王林
Release: 2023-06-12 09:07:03
Original
1571 people have browsed it

With the continuous advancement of Internet technology, more and more websites and applications require file upload operations. In this context, ThinkPHP6, as an excellent PHP framework, also provides a convenient operation method for uploading multiple files. This article will introduce how to perform multiple file upload operations in ThinkPHP6.

1. Related codes for uploading files

In ThinkPHP6, the code for uploading files is located in the controller file. The following is a piece of code for uploading a single file:

public function upload()
{
    //获取上传的文件对象
    $file = request()->file('file');
    //将上传的文件移动到指定目录
    $info = $file->move('./uploads');
    if ($info) {
        //上传成功,返回文件名和路径
        return json(['code' => 0, 'msg' => '上传成功', 'data' => ['file_name' => $info->getFilename(), 'file_path' => '/uploads/'.$info->getSaveName()]]);
    } else {
        //上传失败,返回错误信息
        return json(['code' => 1, 'msg' => $file->getError()]);
    }
}
Copy after login

2. Steps for uploading multiple files

Next, we will introduce how to upload multiple files in ThinkPHP6. The specific steps are:

1. Add multiple file upload boxes in the front-end interface and set the name attribute to the same value.

<form enctype="multipart/form-data" method="post" action="#">
    <input type="file" name="files[]" multiple>
    <button type="submit">上传</button>
</form>
Copy after login

2. Set the code for uploading multiple files in the controller file.

public function upload()
{
    $files = request()->file('files');
    $data = array();
    foreach ($files as $file) {
        $info = $file->validate(['size' => 1024*1024*10, 'ext' => 'jpg,png,gif'])->move('./uploads');
        if ($info) {
            $data[] = ['file_name' => $info->getFilename(), 'file_path' => '/uploads/'.$info->getSaveName()];
        } else {
            return json(['code' => 1, 'msg' => $file->getError()]);
        }
    }
    return json(['code' => 0, 'msg' => '上传成功', 'data' => $data]);
}
Copy after login

Among them, request()->file('files') can obtain multiple uploaded file objects, and process the operation of each uploaded file through loop traversal.

3. Parameter settings for uploading multiple files

In order to ensure the safety and legality of uploading multiple files, we can also set some parameters to limit the size and type of uploaded files, etc. For example:

1. Limit the size of a single file

In the sample code of this article, we pass validate(['size' => 1024102410, 'ext ' => 'jpg,png,gif']) to set the size of a single uploaded file to not exceed 10M.

2. Limit file types

In the validate() function, you can also limit the uploaded file type by setting ext, for example: 'ext' => 'jpg, png, gif'

3. Rename the uploaded file

You can set the name of the uploaded file to a unique random number through

$info = $file->move('./uploads', md5(uniqid()));
Copy after login

.

4. Summary

This article introduces how to perform multiple file upload operations in ThinkPHP6. You need to use request()->file('files') to obtain multiple uploaded files. Object, use a foreach loop to process each uploaded file. At the same time, we can also limit the size and type of uploaded files by setting parameters to ensure the security and legality of multiple uploaded files.

The above is the detailed content of How to perform multiple file upload operations in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template