How to use PHP functions for file upload and download?
In modern web development, file upload and download are very common functions. For PHP developers, PHP provides some built-in functions to handle file operations, including uploading and downloading. This article will introduce how to use PHP functions for file upload and download, and provide code samples for reference.
1. File upload
File upload refers to the process of transferring local files to the server. In PHP, there are two important functions for file upload: move_uploaded_file()
and $_FILES
global variables.
First, we need to add an input control for file upload in the HTML form:
<form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form>
Next, in PHP, use the $_FILES
global variable to access the upload file information. $_FILES
is an associative array that contains various attributes of the uploaded file, such as file name, file type, temporary file path, etc.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['file']; // 获取文件名 $filename = $file['name']; // 获取文件临时存储路径 $tmpPath = $file['tmp_name']; // 上传文件 $destination = 'uploads/' . $filename; move_uploaded_file($tmpPath, $destination); echo 'File uploaded successfully!'; }
In the above code, first obtain the file information through $_FILES['file']
, and then use the move_uploaded_file()
function to move the temporary file to the specified Target path. Finally, the echo
statement is used to output a prompt message indicating that the upload is successful.
2. File Download
File downloading refers to the process of transferring files on the server to the client. In PHP, use the readfile()
function to download files. The following is a basic file download example:
$file = 'path/to/file.txt'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); readfile($file); exit; } else { echo 'File not found!'; }
In the above code, first determine whether the file to be downloaded exists through the file_exists()
function. If the file exists, set the corresponding HTTP header information, including file type, file name, file length, etc. Finally, use the readfile()
function to read the file content and output it to the client.
It should be noted that in order for the browser to correctly parse the file type and file name, the Content-Type
and Content-Disposition
header information needs to be set.
Conclusion
This article introduces how to use PHP functions to upload and download files. In terms of file upload, the function of uploading local files to the server can be realized through the $_FILES
global variable and the move_uploaded_file()
function. In terms of file downloading, the function of downloading server files to the client can be achieved by setting HTTP header information and using the readfile()
function. I hope this article helps you in your development.
The above is the detailed content of How to use PHP functions for file upload and download?. For more information, please follow other related articles on the PHP Chinese website!