With the widespread application of small programs, more and more developers need to interact with backend servers for data. One of the most common business scenarios is to upload files. This article will introduce the PHP background implementation method to implement file upload in a mini program.
1. File upload in the mini program
Implementing file upload in the mini program mainly relies on the mini program API wx.uploadFile(). The API accepts an options object as a parameter, which contains the file path to be uploaded, other data that needs to be passed, and callback functions for upload success and failure.
Code example:
wx.uploadFile({ url: 'http://www.example.com/upload.php', filePath: tempFilePath, name: 'file', formData: { 'user': 'test' }, success: function(res){ console.log(res.data) }, fail: function(res){ console.log(res) } })
In the above code, url is the address where the uploaded file is received in the background, filePath is the file path that needs to be uploaded, and formData is other data that needs to be passed.
2. PHP background code implementation
In the PHP background, there are many ways to upload files. This article mainly introduces the two most common methods-using the $_FILES super global variable and Read the data in the request body directly.
When using the wx.uploadFile() method to upload a file, the $_FILES super global variable can be used in the PHP background code to obtain the upload file information. $_FILES is an associative array through which you can access uploaded file information, including file name, file type, file size, temporary file path, and whether the upload was successful.
Code example:
<?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]); echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB<br>"; } ?>
In the above code, first determine whether the file upload is successful, and if successful, move the file to the specified directory. When uploading files in the applet, we need to set the name parameter to file.
In addition to using the $_FILES super global variable to obtain the uploaded file information, we can also directly read the data in the request body data. The specific implementation method is to obtain the data in the request body by reading the php://input super global variable and save it to the specified file.
Code example:
<?php $file = file_get_contents("php://input"); $filePath = "uploads/" . $_GET["filename"]; // 文件保存的路径 file_put_contents($filePath, $file); echo "Upload successfully!"; ?>
In the above code, first use the file_get_contents() function to read the data in the request body and write it to the specified file. When uploading a file, we need to pass a filename parameter through the URL to specify the path and file name where the file is saved.
3. Summary
To implement file upload in the mini program, the PHP language is used in the background. The common implementation method is to use the $_FILES super global variable and directly read the data in the request body. Through the above code examples, I believe you have understood how to implement file upload in mini programs in PHP.
It should be noted that file upload involves issues such as file security and file upload size restrictions. We need to implement corresponding security measures and restrictions in the background code. Hope this article helps you!
The above is the detailed content of PHP implementation method of file upload in mini program. For more information, please follow other related articles on the PHP Chinese website!