Upload
Preliminary knowledge: Javascript, PHP (small)/perl (few)!
Test environment: Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 failure, estimated to be configured out of the problem)
Directory structure:
/wwwroot/cgi-bin/fileup.php (File receive)
/wwwroot/www/test/phpfileup.htm (file submission)
/wwwroot/www/test/tmp/(Default save directory)
Foreword: File uploads, simple and troublesome. Here is the Upload file Submission page, use this page you can not only generate 1000 upload file box (actually any number of 0~n), and can point out their save path respectively.
The file input box for the Submit page is named: File0,file1,... file100,... filen
The File path box for the Submit page is named: path0,path1,... path100,... pathn
Because the generation of the page is very simple, it is not explained here, two functions are defined with JavaScript, check () is used to submit the page, create () is used to generate the File upload box. If you have any better advice or have any questions please email:gearsoft@netease.com
Phpfileup.htm
--------------------------------------------------------
<title></title>
<style>
. lbtn {FONT-FAMILY:VERDANA;FONT-SIZE:10.5PT}
. ist {font-family:verdana;font-size:14.8px;size:400}
</style>
<script language= "JavaScript" >
function Check () {
Document.frm1.submit ();
Document.body.innerhtml= "Uploading please wait!";
}
var i=0;
function Create () {
var sfrm = Document.frm1.innerHTML;
var icnt = Cnt.value;
for (j=0;j<icnt;j++)
{
SFRM = sfrm + "Please select File" +i+ "<input type=file name= ' file" +i+ "' class=ist>";
SFRM = sfrm + "path: <input type=text name= ' path" +i+ "' value= '. /www/test/tmp/' class=ist> ';
SFRM = sfrm + "<br>";
i++;
}
Document.frm1.innerHTML = sfrm;
Document.frm1.cnt.value=i;
}
</script>
<body class=lbtn >
Please enter the number of files to upload: <input type=text name=cnt value= "ten" Class=ist >
<input Type=button name=bt1 value= "Generate Upload File box" class=lbtn>
<input Type=button name=bt1 value= "Upload" class=lbtn>
<input Type=button name=bt1 value= "Clear" class=lbtn>
<form name=frm1 method= "POST" enctype= "Multipart/form-data" action= ". /.. /cgi-bin/fileup.php ">
<input type=hidden name=cnt value= "class=ist>"
</form>
<input Type=button name=bt1 value= "Upload" class=lbtn>
<input Type=button name=bt1 value= "Clear" class=lbtn>
</body>
The file submission page has been generated and the following task is clear: Save the submitted file contents to the server.
Here's a couple of ways to do this:
1. Use PHP to Save:
We first define a file Save function fup () it has two parameters:
$filename: File contents
$fname: File name (including path)
All that's left is to write a loop to write the file to the server sequentially. Here's a quick explanation:
PHP for upload file processing is like this: if the submitted file box I for FILE0, then the file submitted to PHP saved 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 down the content submitted by the submission 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 "<br>";
Fup ($ $FFNN, $ $ffpath. $ $ffnnname); //".. /www/test/tmp/"
}
?>
----------------------------------------------------------------------
2. Use PERL to Save:
They implement exactly the same principle, not to say much, see 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".
"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;
#<<<<<<<<<<<<<<<<<<<<< The following is a custom process <<<< <<<<<<<<<<<<<<<<<<<<<
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".
"Exit 0;
} Binmode $xfile;
Use file::copy;
Copy ($xfile, $writed. $strNetFname);
}
Sub g_head{
print ' <table border=1 align=center> ';
print ' <tr><td colspan=3 align=center> File Upload results (Upload result) </td></tr> ';
print ' <tr align=center> ';
print ' <td>SourceFile:</td> ';
print ' <td>DestFile:</td> ';
print ' <td>Upload</td> ';
print ' </tr> ';
}
Sub g_body{
print ' <tr> ';
print ' <td> '. $xfile. ' </td> ';
print ' <td> ' $writed. $strNetFname. ' </td> ';
print ' <td>OK!</td> ';
print ' </tr> ';
}
Sub g_bott{
print ' </table> ';
}