亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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?

Aug 05, 2023 pm 07:25 PM
File Upload Download Document php backend

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.

  1. Create HTML form

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>
  1. Create the upload.php file

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.

  1. Create file saving directory

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.

  1. Create the download.php file

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.

  1. Create a downloadable file

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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

Python opening operation after downloading the file Python opening operation after downloading the file Apr 03, 2024 pm 03:39 PM

Python provides the following options to open downloaded files: open() function: open the file using the specified path and mode (such as 'r', 'w', 'a'). Requests library: Use its download() method to automatically assign a name and open the file directly. Pathlib library: Use write_bytes() and read_text() methods to write and read file contents.

How to use Laravel to implement file upload and download functions How to use Laravel to implement file upload and download functions Nov 02, 2023 pm 04:36 PM

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

How to solve Java file upload exception (FileUploadException) How to solve Java file upload exception (FileUploadException) Aug 18, 2023 pm 12:11 PM

How to solve Java file upload exception (FileUploadException). One problem that is often encountered in web development is FileUploadException (file upload exception). It may occur due to various reasons such as file size exceeding limit, file format mismatch, or incorrect server configuration. This article describes some ways to solve these problems and provides corresponding code examples. Limit the size of uploaded files In most scenarios, limit the file size

How to trigger file download when clicking HTML button or JavaScript? How to trigger file download when clicking HTML button or JavaScript? Sep 12, 2023 pm 12:49 PM

Nowadays, many applications allow users to upload and download files. For example, plagiarism detection tools allow users to upload a document file that contains some text. It then checks for plagiarism and generates a report that users can download. Everyone knows how to use inputtypefile to create a file upload button, but few developers know how to use JavaScript/JQuery to create a file download button. This tutorial will teach you various ways to trigger a file download when an HTML button or JavaScript is clicked. Use HTML's <a> tag and download attribute to trigger file download when the button is clicked. Whenever we give the <a> tag

File Uploading and Processing in Laravel: Managing User Uploaded Files File Uploading and Processing in Laravel: Managing User Uploaded Files Aug 13, 2023 pm 06:45 PM

File Uploading and Processing in Laravel: Managing User Uploaded Files Introduction: File uploading is a very common functional requirement in modern web applications. In the Laravel framework, file uploading and processing becomes very simple and efficient. This article will introduce how to manage user-uploaded files in Laravel, including verification, storage, processing, and display of file uploads. 1. File upload File upload refers to uploading files from the client to the server. In Laravel, file uploads are very easy to handle. first,

How to implement FTP file upload progress bar using PHP How to implement FTP file upload progress bar using PHP Jul 30, 2023 pm 06:51 PM

How to use PHP to implement FTP file upload progress bar 1. Background introduction In website development, file upload is a common function. For the upload of large files, in order to improve the user experience, we often need to display an upload progress bar to the user to let the user know the file upload process. This article will introduce how to use PHP to implement the FTP file upload progress bar function. 2. The basic idea of ??implementing the progress bar of FTP file upload. The progress bar of FTP file upload is usually calculated by calculating the size of the uploaded file and the size of the uploaded file.

How to use Hyperf framework for file downloading How to use Hyperf framework for file downloading Oct 21, 2023 am 08:23 AM

How to use the Hyperf framework for file downloading Introduction: File downloading is a common requirement when developing web applications using the Hyperf framework. This article will introduce how to use the Hyperf framework for file downloading, including specific code examples. 1. Preparation Before starting, make sure you have installed the Hyperf framework and successfully created a Hyperf application. 2. Create a file download controller First, we need to create a controller to handle file download requests. Open the terminal and enter

See all articles