Getting Started with PHP: File Uploading and Downloading

WBOY
Release: 2023-05-22 11:16:02
Original
1469 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:

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传文件">
</form>
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:

<?php
if(isset($_FILES['file'])){
    $file = $_FILES['file'];
    $filename = $file['name'];
    $tmp_name = $file['tmp_name'];
    $target_path = "./uploads/".$filename;
    move_uploaded_file($tmp_name, $target_path);
    echo "文件上传成功";
}
?>
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:

<a href="download.php?filename=test.pdf">点击下载文件</a>
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:

<?php
if(isset($_GET['filename'])){
    $filename = "./uploads/".$_GET['filename'];
    if(file_exists($filename)){
        header('Content-type: application/pdf');
        header('Content-Disposition: attachment; filename="'.basename($filename).'"');
        readfile($filename);
        exit;
    }else{
        echo "文件不存在";
    }
}
?>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template