Home  >  Article  >  Backend Development  >  How to use PHP to implement file upload and download functions

How to use PHP to implement file upload and download functions

王林
王林Original
2023-09-06 10:51:231679browse

如何使用 PHP 实现文件上传和下载功能

How to use PHP to implement file upload and download functions

When developing a website, file upload and download functions are very common requirements. As a popular server-side scripting language, PHP provides a wealth of functions and class libraries to help us implement these functions. This article will introduce how to use PHP to implement file upload and download functions, including basic file upload and download, and how to handle uploaded files and downloaded files.

1. File upload function

  1. Create a file upload form

First, we need to create an HTML form to implement the file upload function. Create a file named upload.html and add the following code:

<html>
<head>
<title>文件上传</title>
</head>
<body>

<form action="upload.php" method="POST" enctype="multipart/form-data">
选择文件:<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="上传文件" name="submit">
</form>

</body>
</html>
  1. Create a file upload processing script

Next, we need to create a file named upload .php PHP script to handle file uploads. Create a file with the following code:

<?php
$targetDir = "uploads/"; // 上传文件保存的目录

if(isset($_POST["submit"])) {
   $targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]); // 上传文件的完整路径

   if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
       echo "文件已成功上传。";
   } else {
       echo "文件上传失败。";
   }
}
?>

The above code first specifies the directory where the file upload is saved, which is the uploads directory. When handling file uploads, we use the move_uploaded_file function to move the temporary file to the target path.

  1. Test the file upload function

Now, we can test the file upload function by opening the upload.html file in the browser, selecting a file and clicking the upload button. Uploaded files will be saved in the uploads directory.

2. File download function

  1. Create a file download link

First, we need to create a file download link to allow users to download files. Suppose we already have a file saved in the uploads directory and named test.pdf. We can create a download link through the following code:

<a href="download.php?file=test.pdf">下载文件</a>
  1. Create a file download processing script

Then, we need to create a PHP script named download.php Handle file downloads. Create a file with the following code:

<?php
$fileDir = "uploads/"; // 文件保存的目录

if(isset($_GET["file"])) {
   $filePath = $fileDir . $_GET["file"]; // 文件的完整路径

   if(file_exists($filePath)) {
       header("Content-Type: application/octet-stream");
       header("Content-Disposition: attachment; filename=" . basename($filePath));
       header("Content-Length: " . filesize($filePath));

       readfile($filePath);
   } else {
       echo "文件不存在。";
   }
}
?>

The above code first specifies the directory where the file is saved, which is the uploads directory. When processing file downloads, we set some response header information, such as Content-Type, Content-Disposition, and Content-Length, and then use the readfile function to output the file content to the browser.

  1. Test the file download function

Now, we can click the download link in the browser to test the file download function. The browser will start downloading the file, saving it locally.

Summary

Through the above steps, we have learned how to use PHP to implement file upload and download functions. The file upload function can be implemented by creating a file upload form and corresponding processing script. The file download function can be implemented by creating a file download link and corresponding processing script. In actual projects, we can expand and optimize the file upload and download functions according to needs, such as adding file type verification, file size limits, etc.

I hope this article can help you understand and apply PHP to implement file upload and download functions!

The above is the detailed content of How to use PHP to implement file upload and download functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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