Home > Backend Development > PHP Tutorial > PHP and CGI file upload and download technology: how to implement file management functions

PHP and CGI file upload and download technology: how to implement file management functions

王林
Release: 2023-07-21 11:22:01
Original
1481 people have browsed it

File upload and download technology of PHP and CGI: How to implement file management functions

Introduction:
File upload and download is one of the common functions in modern web applications. This article will introduce how to implement file upload and download functions using PHP and CGI programming languages, and show some code examples to demonstrate how to manage uploaded and downloaded files.
The following is what we will cover:

  1. Basic concepts of file upload
  2. PHP implementation of file upload
  3. CGI implementation of file upload
  4. Basic concept of file download
  5. PHP implementation file download
  6. CGI implementation file download
    Let’s get started!
  7. Basic concept of file upload:
    File upload is the process of transferring files from the client computer to the server. File upload is usually used by users to submit multimedia files such as pictures, documents, audio or videos to the server.
  8. PHP to implement file upload:
    The following is a simple PHP code example that demonstrates how to use PHP to implement the file upload function.
<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!";
}
?>
Copy after login

In the above code, we first create an HTML form through which the user can select the file to upload. In the form, we use the <input type="file"> element to receive the file selected by the user.
When the user clicks the "Upload File" button, the form data will be submitted to the upload.php file for processing.
In the upload.php file, we first define the path to the target upload directory, and then use the move_uploaded_file() function to move the uploaded file to the target directory.

  1. CGI file upload:
    The following is an example of a CGI script that demonstrates how to use CGI to implement the file upload function.
#!/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!";
}
Copy after login

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

  1. Basic concepts of file downloading:
    File downloading is the process of transferring files from the server to the client computer. File downloads are often used to let users get the files they need from a web application.
  2. PHP to implement file download:
    The following is a simple PHP code example that demonstrates how to use PHP to implement the file download function.
<?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);
?>
Copy after login

In the above code, we first specify the path to the file. Then, use the header() function to set the HTTP response header information 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.

  1. CGI implementation of file download:
    The following is an example of a CGI script that demonstrates how to use CGI to implement the file download function.
#!/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;
Copy after login

In this example, we first specify the path to the file we want to download. Then, use the header() function to set the HTTP response header information and download the file as an attachment.
Finally, we use the open() function to open the file, and then use the print function to output the file contents to the client line by line.

Summary:
This article introduces how to use PHP and CGI programming languages ​​to implement file upload and download functions. We provide 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 functionality in your web applications.

The above is the detailed content of PHP and CGI file upload and download technology: how to implement file management functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template