Getting Started with PHP: File Uploading and Downloading

WBOY
Release: 2023-05-22 11:16:02
Original
1381 people have browsed it

In web development, file uploading and downloading is a very common requirement. Whether users upload avatars or documents, or administrators ask users to download a file, this function is needed. As a powerful server-side language, PHP naturally provides powerful file operation functions and class libraries, allowing us to easily implement file upload and download functions.

This article will introduce the basic process and common functions to implement file upload and download in PHP, and provide sample code. If you are a PHP beginner or are learning file operations, I hope this article can provide you with some guidance and help.

File upload

In PHP, file upload is implemented through the HTTP protocol, that is, through a form, the user can select a local file and upload it to the server. The functions we need to use are the $_FILES global variable and the move_uploaded_file() function.

First, we need to write an HTML form on the front end. The code is as follows:

Copy after login

It should be noted that we need to set the method of the form to post, the action to the php file for background processing file upload, and the enctype needs to be set to multipart/form-data to support files Upload.

Next, we write code in the upload.php file in the background to handle file upload. The code is as follows:

Copy after login

Use if to determine whether a file has been uploaded, and obtain various information about the file from the $_FILES array. $file['name'] represents the file name, $file['tmp_name'] represents the file path of the temporary file. We need to move it to the specified location on the server, assuming it is the "./uploads/" directory, and finally output a successful upload message.

File download

File download means that the user can download a file from the server to the local by clicking a link or button. The functions we need to use are the header() function and readfile() function.

First, we need to set the access permissions of the files on the server so that users can download them. We can set it through the .htaccess file or the configuration file of the web server.

Next, we write HTML code on the front end to generate the download link. The code is as follows:

点击下载文件
Copy after login

It should be noted that we need to set the href of the link to the php file downloaded by the background processing file, and also need to bring the file name as a parameter in the link.

Finally, we write code in the download.php file in the background to handle file downloads. The code is as follows:

Copy after login

Use if to determine whether there is a file name parameter passed in, and generate the absolute path of the file based on the file name. If the file exists, we need to set Content-type to the file type (assumed to be pdf type here) and Content-Disposition to attachment, which means downloading as an attachment and outputting the file content. Finally, add exit to prevent the browser from outputting other content.

Summary

Through the brief introduction of this article, I believe that readers will have a deeper understanding of file upload and download in PHP. It should be noted that both file upload and download operations need to consider security and user experience, such as limiting the size and type of uploaded files, anti-leeching of download links, etc. I hope readers will also pay attention to these details when implementing file operations.

The above is the detailed content of Getting Started with PHP: File Uploading and Downloading. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!