


How to solve the problem that php cannot accept multiple image uploads in uniapp
1. Background introduction
With the continuous development of Internet technology, more and more Web applications need to support the image upload function. Uniapp is a very popular mobile development framework, which is cross-platform, efficient, and easy to use. However, when we use uniapp to develop the multi-image upload function, we will encounter some problems: the server cannot correctly receive the request and cannot obtain the passed image information. This article will explore possible causes and solutions to this problem.
2. Problem description
We use the upload component plug-in provided by uniappuni-upload
to develop the multi-image upload function and use PHP back-end code to receive upload requests and save image information. The upload page code is as follows:
<template> <view> <view> <image></image> </view> <view> <view> <image></image> <view>删除</view> </view> </view> <view> 提交 </view> </view> </template> <script> import uniUpload from "@/components/uni-upload/uni-upload.vue"; export default { components: { uniUpload }, data() { return { fileList: [], }; }, methods: { chooseImage() { uni.chooseImage({ count: 9, success: (res) => { this.fileList = [...this.fileList, ...res.tempFilePaths.map((path) => ({ path }))]; }, }); }, deleteImage(index) { this.fileList.splice(index, 1); }, submit() { const formData = new FormData(); this.fileList.forEach((item, index) => { formData.append(`file${index}`, item.path); }); uni.request({ method: "POST", url: "http://localhost/upload.php", header: { "Content-Type": "multipart/form-data", }, data: formData, success: (res) => { console.log("upload success", res.data); }, fail: (error) => { console.log("upload fail", error); }, }); }, }, }; </script>
The upload component uses the uni-upload
plug-in, which calls the album to select pictures through the chooseImage
method, and adds tempFilePaths
to Fill in the image path in fileList
, and upload the image path in fileList
to the backend server in the submit
method.
On the server side, we use PHP as the back-end language, the code is as follows:
<?php $upload_dir = "uploads/"; if (!file_exists($upload_dir)) { mkdir($upload_dir); } foreach ($_FILES as $key => $file) { $tmp_name = $file["tmp_name"]; $name = $file["name"]; if (move_uploaded_file($tmp_name, $upload_dir . $name)) { echo "upload success "; } else { echo "upload fail "; } } ?>
It was found in the local test that after submitting the image, the back-end server could not correctly read the upload request and could not Save pictures correctly. Some solutions will be proposed below.
3. Cause of the problem
According to the error message, it may be caused by incorrect file upload method. FormData and multipart/form-data are now an important way to implement file upload through forms. However, if the appropriate request header information is not set, the server cannot correctly obtain the uploaded file, which may be the cause of this problem.
4. Solution
- Change the request header information
Add the header content type and boundary in the upload request, where type is when sending the request The boundary part of the Content-Type is a random string that splits the data.
uni.request({
method: "POST",
url: "http://localhost/upload.php",
header: {
"Content-Type": "multipart/form-data; boundary=" + Math.random().toString().substr(2),
},
data: formData,
success: (res) => {
console.log("upload success", res.data);
},
fail: (error) => {
console.log("upload fail", error);
} ,
});
- Change the key name of the uploaded file
On the client, we assemble the file list into formData through form data append. At this time, the key of each file defaults to its position in formData, which is an increasing number starting from 0. The key received by the server may be the name
value specified in the upload component. Therefore, when uploading files, you can specify a key name for each file, as follows:
this.fileList. forEach((item, index) => {
formData.append("file" index, item.path);
});
Because of the file
here It is different from the name
attribute value of the uploaded component, so it should also be modified accordingly during back-end processing.
foreach($_FILES as $file) {
$tmp_name = $file["tmp_name"];
$name = $file["name"];
if (move_uploaded_file( $tmp_name, $upload_dir . $name)) {
echo "upload success ";
} else {
echo "upload fail ";
}
}
- Higher versions of PHP need to add parameters and modify php .ini
For higher versions of PHP, you need to add the following parameters to the php.ini
file:
post_max_size=20M
upload_max_filesize=20M
max_execution_time=600
max_input_time=600
After the setting is completed, Apache needs to be restarted to take effect.
4. Summary
This article discusses the problem that the image information transmitted when uploading multiple images in uniapp cannot be received by PHP. By modifying the request header information, changing the key name of the uploaded file and configuring PHP .ini file, etc., successfully solved the problem. Finally, it is recommended that web developers pay attention to effective testing of the upload function when using uniapp for mobile application development to reduce unnecessary errors and losses.
The above is the detailed content of How to solve the problem that php cannot accept multiple image uploads in uniapp. 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.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
