How to perform multiple file upload operations in ThinkPHP6?
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()]); } }
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>
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]); }
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()));
.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
