In modern web development, file uploads and downloads are common functionalities. PHP, as a popular server-side language, provides various built-in functions to handle file operations, including file uploads and downloads. This article will explain in detail how to implement these two functions in PHP, with example code to help developers better understand and apply them.
File upload refers to the process of transferring a file from the local machine to the server. PHP provides two important functions, `move_uploaded_file()` and the `$_FILES` global variable, to handle file uploads.
First, you need to add a file input control to the HTML form for file upload:
<form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form>
Then, in PHP, you can access the uploaded file information through the `$_FILES` global variable. `$_FILES` is an associative array containing various attributes of the uploaded file, such as the file name, file type, and temporary file path. Here is the code example for file upload:
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['file']; // Get the file name $filename = $file['name']; // Get the temporary storage path of the file $tmpPath = $file['tmp_name']; // Set the destination storage path $destination = 'uploads/' . $filename; // Move the file to the destination path move_uploaded_file($tmpPath, $destination); echo 'File uploaded successfully!'; }
In this code, we first retrieve the file information through `$_FILES['file']`, then use the `move_uploaded_file()` function to move the temporary file to the specified destination path. Finally, we output a success message using `echo`.
File download refers to the process of transferring a file from the server to the client. PHP provides the `readfile()` function to achieve file download. Here is a basic code example for file download:
$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 this code, we first check if the file exists using `file_exists()`. If the file exists, we set the necessary HTTP headers, including file type, file name, and file length, to ensure the file is downloaded correctly. Then, we use `readfile()` to output the file content to the client.
Note that in order for the browser to properly handle the file type and file name, we need to set the appropriate HTTP headers, especially `Content-Type` and `Content-Disposition`.
This article explained how to handle file upload and download using PHP functions. With the `$_FILES` global variable and the `move_uploaded_file()` function, developers can easily implement file upload functionality. The `readfile()` function, along with setting the correct HTTP headers, can be used to implement file download functionality. These tips will save you a lot of time in development and help you perform file operations more efficiently.