Current Location: Home> Latest Articles> PHP and CGI File Upload and Download: A Detailed Guide with Code Examples

PHP and CGI File Upload and Download: A Detailed Guide with Code Examples

M66 2025-06-16

PHP and CGI File Upload and Download: A Detailed Guide with Code Examples

File upload and download are common features in modern web applications. This article introduces how to implement file upload and download functionality using PHP and CGI programming languages, and provides code examples to demonstrate how to manage uploaded and downloaded files.

The following topics will be covered:

  1. Basic Concepts of File Upload
  2. PHP Implementation of File Upload
  3. CGI Implementation of File Upload
  4. Basic Concepts of File Download
  5. PHP Implementation of File Download
  6. CGI Implementation of File Download

Basic Concepts of File Upload:

File upload is the process of transferring a file from the client's computer to the server. It is commonly used for users to submit images, documents, audio, or video files to the server.

PHP Implementation of File Upload:

Here is a simple PHP code example demonstrating how to implement file upload functionality using PHP.

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

<?php
if (isset($_POST["submit"])) {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    echo "File uploaded successfully!";
}
?>

In the code above, we first create an HTML form where users can select the file they want to upload. When the user clicks the "Upload File" button, the form data is submitted to the `upload.php` file for processing. In the `upload.php` file, we define the target upload directory path and use the `move_uploaded_file()` function to move the uploaded file to the target directory.

CGI Implementation of File Upload:

Here is an example of a CGI script that demonstrates how to implement file upload functionality using CGI.

#!/usr/bin/perl
use CGI;

my $cgi = CGI->new;
print $cgi->header;
print <<END_HTML;
    <form action="/cgi-bin/upload.cgi" enctype="multipart/form-data" method="post">
        <input type="file" name="fileToUpload">
        <input type="submit" value="Upload File" name="submit">
    </form>
END_HTML

if ($cgi->param("submit")) {
    my $upload_dir = "/path/to/uploads/";
    my $file = $cgi->upload("fileToUpload");
    my $filename = $cgi->param("fileToUpload");
    my $target_file = $upload_dir . $filename;
    open(my $fh, '>', $target_file) or die $!;
    while (my $bytesread = read($file, my $buffer, 1024)) {
        print $fh $buffer;
    }
    close $fh;
    print "File uploaded successfully!";
}

The above code is a Perl CGI script. By using the CGI module, we can easily handle form data. In the script, we first create an HTML form where users can select the file to upload. When the user clicks the "Upload File" button, the form data is submitted to the `upload.cgi` script for processing. In the script, we define the file upload directory path and use the `upload()` function to save the uploaded file to the target file.

Basic Concepts of File Download:

File download is the process of transferring a file from the server to the client's computer. It is commonly used to allow users to obtain files from a web application.

PHP Implementation of File Download:

Here is a simple PHP code example demonstrating how to implement file download functionality using PHP.

<?php
$file = "path/to/file.pdf";
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
header("Content-Length: " . filesize($file));
readfile($file);
?>

In the above code, we first specify the file's path. Then, we use the `header()` function to set the HTTP response headers to instruct the browser to download the file as an attachment. Finally, we use the `readfile()` function to read and output the file content to the client.

CGI Implementation of File Download:

Here is an example of a CGI script that demonstrates how to implement file download functionality using CGI.

#!/usr/bin/perl
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = CGI->new;
my $file = "/path/to/file.pdf";

print $cgi->header(-type=>'application/octet-stream', -attachment=>basename($file));
open(my $fh, '<', $file) or die $!;
binmode($fh);
while (<$fh>) {
    print $_;
}
close $fh;

In this example, we first specify the file's path. Then, we use the `header()` function to set the HTTP response headers to download the file as an attachment. Finally, we use the `open()` function to open the file, and the `print` function to output the file content line by line to the client.

Summary:

This article covered how to implement file upload and download functionality using PHP and CGI. We provided code examples to help readers understand how to manage uploaded and downloaded files. By learning and using these technologies, you can easily implement file management features in your web applications.