Pre-Knowledge: Javascript, PHP (small)/perl (small)!
Test environment: Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 failed, estimated to be a problem with the configuration)
Directory structure:
/wwwroot/cgi-bin/fileup.php (File receive)
/wwwroot/www/test/phpfileup.htm (file submission)
/wwwroot/www/test/tmp/(Default save directory)
Preface: File upload, simple and troublesome. The following is the upload file Submission page, using this page you can not only generate 1000 upload file box (actually any number of 0~n), and can indicate their save path respectively.
The file input box for the submission page is named: File0,file1,... file100,... filen
The File path box for the Submit page is named: path0,path1,... path100,... pathn
Because the page generation is very simple, so there is no more explanation, with JavaScript defined two functions, check () for the submission page, create () to generate File upload box. If you have any better suggestions or have any questions please email:gearsoft@netease.com
Phpfileup.htm
--------------------------------------------------------
Please enter the number of files to upload:
The file submission page has been generated, and the following task is clear: Save the submitted file contents to the server.
Here are two ways to accomplish this:
1. Use PHP to Save:
We first define a file Save function fup () it has two parameters:
$filename: File contents
$fname: File name (include path)
The rest is to write a loop to write the file to the server sequentially. Here is a brief explanation:
PHP's handling of uploading files is this: If the submitted file box I am FILE0, then the file content submitted to PHP is stored in the variable $file 0, and the file name is stored in the $file 0_name. So what I'm going to do in this loop is to break up the submission of the commit page and see the code below for the implementation process.
fileup.php
----------------------------------------------------------------------
function Fup ($filename, $fname)
{If ($filename! = "None") {
Copy ($filename, $fname);
Unlink ($filename);
}
}
for ($i =0; $i < $cnt; $i + +)
{
$FFNN = "file". $i;
$ffnnname = $ffnn. " _name ";
$ffpath = "path". $i;
Print $ $FFNN;
Print $ $ffnnname;
Print "
";
Fup ($ $FFNN, $ $ffpath. $ $ffnnname); //".. /www/test/tmp/"
}
?>
----------------------------------------------------------------------
2. Use PERL to Save:
They are implemented exactly the same way, not much here, see the code:
fileup.cgi (fileup.pl)
----------------------------------------------------------------------
#!/usr/bin/perl
Use CGI qw/:standard/;
if ($ENV {content_type}!~/multipart/form-data/) {
Print "Cache-control:no-cachenpragma:no-cachen".
"Content-type:text/htmlnn".
"Your Web browser cannot upload files. Sorry. ";
Exit 0;
}
$cntfile =param (CNT);
Print header;
Print start_html;
#print "Receiving please wait ...";
&g_head;
# $writed =: /www/test/tmp/;
for ($i =0; $i < $cntfile; $i + +) {
$paramfile = file. $i;
$parampath =path. $i;
$writed =param ($parampath);
&upfile;
&g_body;
}
&g_bott;
#<<<<<<<<<<<<<<<<<<<<<><>< p=""><>
Sub Upfile
{
$maxdata = 512000;
# $writed =: /www/test/tmp/;
$strRFname =reverse $xfile;
$intIndex =index ($strRFname,);
$strNetFname =substr ($strRFname, 0, $intIndex);
$strNetFname =scalar Reverse $strNetFname;
if (stat $xfile) [7]> $maxdata) {
Print "status:411 Size not Allowedn".
"CONTENT-TYPE:TEXT/HTMLNALLOW:POSTNN".
"411 411 Size Not allowed
You got big problem. Try again.
n ";
Exit 0;
}
Binmode $xfile;
Use file::copy;
Copy ($xfile, $writed. $strNetFname);
}
Sub g_head{
Print
Sub g_body{
Print
Sub g_bott{
Print
; Print
File upload results (Upload result) |
; Print
; Print
SourceFile: | ; Print
DestFile: | ; Print
Upload | ; Print
; }
; Print
. $xfile. | ; Print
. $writed. $strNetFname. | ; Print
Ok! | ; Print
; }
;
}
----------------------------------------------------------------------
Please email:gearsoft@netease.com if you have any good suggestions.
http://www.bkjia.com/PHPjc/532500.html www.bkjia.com true http://www.bkjia.com/PHPjc/532500.html techarticle Preliminary knowledge: Javascript, PHP (small)/perl (small amount)! Test environment: Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 failed, estimated to be a problem with configuration) directory structure:/wwwroot/cgi- b .....