In modern web development, file upload and download are very common functionalities. PHP provides several built-in functions to help developers handle file operations, including uploading and downloading. This article will explain how to use PHP functions for file uploads and downloads, with code examples for reference.
File upload refers to the process of transferring a local file to the server. In PHP, `move_uploaded_file()` and the `$_FILES` global variable are used for file uploads.
First, make sure your HTML form includes a file input control for file uploading:
<form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form>
Next, in your PHP code, use the `$_FILES` global variable to access the uploaded file's information. `$_FILES` is an associative array that contains various file properties, such as the file name, file type, and temporary file path.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $file = $_FILES['file']; // Get file information $filename = $file['name']; // File name $tmpPath = $file['tmp_name']; // Temporary file path // Upload file $destination = 'uploads/' . $filename; move_uploaded_file($tmpPath, $destination); echo 'File uploaded successfully!'; }
In the code above, we first access the uploaded file's information using `$_FILES['file']`, then use `move_uploaded_file()` to move the temporary file to the designated destination. Finally, we output a success message with `echo`.
File download refers to the process of transferring a file from the server to the client. PHP uses the `readfile()` function to implement file downloads.
Here’s a simple example of how to download a file:
$file = 'path/to/file.txt'; // Set the path to the file to download 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); // Output file content 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 appropriate HTTP headers (such as content type, file name, and file size) and then use `readfile()` to output the file content to the client.
It’s important to set the correct HTTP headers, especially `Content-Type` and `Content-Disposition`, to help the browser correctly handle the file and initiate the download.
This article explained how to use PHP functions for file upload and download. Using the `$_FILES` global variable and `move_uploaded_file()` function, you can easily upload files from your local machine to the server. With the proper HTTP headers and the `readfile()` function, you can download files from the server to the client. I hope these code examples are helpful for your development needs.