Home >Backend Development >PHP Tutorial >How to implement file upload and download in PHP back-end function development?
How to implement file upload and download in PHP back-end function development?
In web development, file upload and download are very common functions. Whether users upload images, documents or download files, back-end code is required to process them. This article will introduce how to implement file upload and download functions on the PHP backend, and attach specific code examples.
1. File upload
File upload refers to transferring files from the local computer to the server. PHP provides a wealth of functions and classes to implement file upload functions.
First, create a form in HTML for users to select files to upload. You can create a file selection button using the input element's type attribute as file.
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form>
Next, we need to create a PHP file to handle the upload request. In the upload.php file, we can use the $_FILES superglobal variable to obtain uploaded file information.
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['file']; // 获取文件名 $filename = $file['name']; // 获取文件临时路径 $tmp_path = $file['tmp_name']; // 指定文件保存的路径 $save_path = 'uploads/' . $filename; // 将文件从临时路径移动到指定路径 move_uploaded_file($tmp_path, $save_path); echo '文件上传成功!'; } ?>
In the upload.php file, we first obtain the uploaded file information through $_FILES['file']. We can then use the move_uploaded_file function to move the file from the temporary path to the specified path. Finally, we can give the user a feedback telling them that the file upload was successful.
In the above code example, we saved the uploaded file in a folder named "uploads". Before creating the upload.php file, we need to ensure that the folder already exists, otherwise the file cannot be saved.
<?php if (!file_exists('uploads')) { mkdir('uploads', 0777, true); } ?>
The above code can be used to check whether the folder named "uploads" exists. If it does not exist, you can use the mkdir function to create the folder.
2. File download
File download refers to downloading files on the server to the local computer. PHP implements file downloading through the header function.
In the download.php file, we need to obtain the path of the file to be downloaded and set the corresponding download header information through the header function.
<?php $file_path = 'files/document.pdf'; if (file_exists($file_path)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file_path)); header('Content-Length: ' . filesize($file_path)); header('Pragma: public'); header('Expires: 0'); readfile($file_path); exit; } else { die('文件不存在!'); } ?>
In the above code example, we set the Content-Disposition header information to attachment through the header function and set the file name to be downloaded. Then, we use the readfile function to read the file content and output it to the user.
In the download.php file, we specify a file named "files/document.pdf" for users to download. Before creating the download.php file, we need to ensure that the file already exists, otherwise the file cannot be downloaded.
3. Summary
Through the introduction of this article, we have learned how to implement file upload and download functions on the PHP backend. File upload requires obtaining the uploaded file information through the $_FILES superglobal variable, and using the move_uploaded_file function to move the file from the temporary path to the specified path. When downloading a file, you can set the corresponding download header information through the header function, and use the readfile function to output the file content to the user. The above code examples can be used as a reference, and developers can make personalized modifications and improvements according to actual needs. Hope this article is helpful to you!
The above is the detailed content of How to implement file upload and download in PHP back-end function development?. For more information, please follow other related articles on the PHP Chinese website!