讓你同時上傳 1000 個檔案_PHP教程

來源:互聯網
上載者:User
預備知識:Javascript, PHP(少量)/Perl(少量)!
測試環境:Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 失敗,估計是配置出了問題)
目錄結構:
/wwwroot/cgi-bin/fileup.php (檔案接收)
/wwwroot/www/test/phpfileup.htm (檔案提交)
/wwwroot/www/test/tmp/ (預設儲存目錄)

前言:檔案上傳,簡單又麻煩。下面是上傳檔案的提交頁面,利用該頁面你不僅可以產生 1000 個上傳檔案框(實際是任意多個 0~n ),而且可分別指出它們的儲存路徑。
提交頁面的檔案輸入框為命名為: file0,file1,...file100,...fileN
提交頁面的檔案路徑框為命名為: path0,path1,...path100,...pathN
由於頁面的產生非常簡單,所以就不在此多解釋了,用 javascript 定義了兩個函數,check() 用於提交頁面,create()用於組建檔案上傳框。如果你有什麼更好的建議或有什麼疑問請 Email: gearsoft@netease.com

phpfileup.htm
--------------------------------------------------------





請輸入要上傳檔案的數量:










檔案提交頁面既已產生,下面任務就很明確了:將提交的檔案內容儲存到伺服器上。

下面我們用兩種方法來實現這個功能:

1. 用 PHP 來儲存:
我們先定義一個檔案儲存函數 fup() 它有兩個參數:
$filename: 檔案內容
$fname: 檔案名稱(包含路徑)
剩下的就是寫一個迴圈將檔案依次寫入伺服器。這裡要簡單說明一下:
PHP 對於上傳檔案的處理是這樣的:如果提交的檔案框我為 file0, 那麼提交給 PHP 的檔案內容儲存在變數 $file0 中,而檔案名稱則儲存在 $file0_name 中。這樣在這個迴圈中我要做的就是將提交頁面提交的內容分解出來,實現過程請看下面的代碼。

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. 用 PERL 來儲存:
它們實現的原理完全一樣,在此不多說,請看代碼:

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;

#<<<<<<<<<<<<<<<<<<<<<以下為自訂過程<<<<<<<<<<<<<<<<<<<<<<<<<

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 ; print ; print ; print ; print ; print ; } ; print ; print ; print ; print ; }
檔案上傳結果(Upload Result)
SourceFile:DestFile:Upload
.$xfile ..$writed.$strNetFname.OK!
;
}
----------------------------------------------------------------------
如有好的建議請 Email: gearsoft@netease.com


http://www.bkjia.com/PHPjc/532500.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532500.htmlTechArticle預備知識:Javascript, PHP(少量)/Perl(少量)! 測試環境:Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 失敗,估計是配置出了問題) 目錄結構: /wwwroot/cgi-b...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.