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:
<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 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.
#!/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 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.
<?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 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.
#!/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 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!