How to handle file upload and download in PHP development?
File uploading and downloading is one of the common functions in web applications. In PHP development, handling file uploads and downloads is relatively simple, just use the built-in functions and specific file operation functions provided by PHP. This article will introduce in detail the methods and precautions on how to handle file upload and download in PHP development.
1. File upload
Add a file upload field in the HTML form
In the HTML form, use thetag to add a file upload field. For example, to upload an image file, you can add the following HTML code:
Receive and process files on the server side
On the server side, you need to write a PHP script to receive and processing uploaded files. First, use the $_FILES superglobal variable to check whether there is a file uploaded and obtain the uploaded file information. For example, to get uploaded image file information, you can use the following PHP code:
$uploadedFile = $_FILES['image']; $fileName = $uploadedFile['name']; $fileType = $uploadedFile['type']; $fileSize = $uploadedFile['size']; $tmpFilePath = $uploadedFile['tmp_name'];
Verify file type and size
After receiving the uploaded file, the file type and size should be verified size to ensure that the uploaded file meets the requirements. This can be verified using the relevant built-in functions for file type and file size. For example, to verify whether the uploaded file type is an image, you can use the following PHP code:
$allowedTypes = array('image/jpeg', 'image/png', 'image/gif'); if (!in_array($fileType, $allowedTypes)) { echo '只允许上传JPEG、PNG或GIF格式的图片文件'; return; } $maxFileSize = 10 * 1024 * 1024; // 最大文件大小为10MB if ($fileSize > $maxFileSize) { echo '文件大小超过了允许的最大限制'; return; }
Move the uploaded file to the target location
After completing the file type and size verification, the upload will Move the files to the specified destination location. This can be achieved using the move_uploaded_file() function. For example, move the uploaded image file to the specified directory:
$uploadDir = 'uploads/'; $targetFilePath = $uploadDir . $fileName; if (move_uploaded_file($tmpFilePath, $targetFilePath)) { echo '文件上传成功'; } else { echo '文件上传失败'; }
2. File download
Provide file download link
In File download links are provided on the web page. You can use thetag to create a download link. For example, to provide a link to download a PDF file named "file.pdf", you can use the following HTML code:
Through the above steps, you can process file uploads in PHP development and download functions. In actual applications, some improvements and extensions can also be made according to specific needs, such as adding more uploaded file verification rules and implementing functions such as file lists and multi-file downloads.
The above is the detailed content of How to handle file upload and download in PHP development?. For more information, please follow other related articles on the PHP Chinese website!