Current Location: Home> Latest Articles> PHP File Upload and Download Guide: Implement File Upload and Download Features

PHP File Upload and Download Guide: Implement File Upload and Download Features

gitbox 2025-07-28

PHP File Upload and Download Features Overview

File upload and download are common functionalities in web development. In PHP, implementing file upload and download operations is quite straightforward. This article will guide you through how to implement these functionalities in PHP, including code examples for uploading and downloading files.

File Upload

File uploads are typically handled by submitting an HTML form, and PHP processes the uploaded file. Two essential PHP functions are involved: move_uploaded_file and $_FILES.

move_uploaded_file is used to move the uploaded file from the temporary directory to the desired target directory, and $_FILES contains details about the uploaded file, such as file name, temporary storage path, etc.

Here’s a basic file upload form:

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="fileToUpload">
  <input type="submit" value="Upload File">
</form>

This form supports file uploads by setting enctype="multipart/form-data".

Now, let’s handle the uploaded file in the PHP script:

<?php
$target_dir = "uploads/"; // The upload directory
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // The upload file path
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "File uploaded successfully";
} else {
    echo "File upload failed";
}
?>

This code moves the uploaded file to the specified directory uploads/ and outputs a success or failure message.

When uploading files, make sure the target directory has the correct permissions, typically set to 755.

File Download

File download can be implemented using the header function in PHP. Below is a simple example of file download:

<?php
$file = 'path/to/file.pdf'; // Path to the file to download
$filename = 'file.pdf'; // The filename when downloading
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($file);
?>

This code uses the Content-Disposition HTTP header to instruct the browser to download the file rather than displaying it in the browser.

Ensure that the file path is correct and has the appropriate read permissions for the download to work properly.

Summary

By using PHP's built-in functions, file upload and download operations are easy to implement. During file upload, you use move_uploaded_file to move the file to a target location, while for file download, you can use header and readfile to output the file. You can also add more features to file uploads and downloads based on your needs, such as restricting file types or sizes.

Once you have mastered these basic PHP file operations, you can extend and optimize your file upload and download functionality according to the requirements of your project.