[PHP] pear 模組 HTTP_Upload 多重檔案上傳 Multiple files upload
Jan 3rd, 2009 by appleboy 參觀者:8,114Views 機器人:591Views
推到 Twitter!
推到 Plurk!
推到 Facebook!
自從上次介紹了 [PHP]好用的上傳 pear 模組 HTTP_Upload,最近又要使用到多重的檔案上傳,就又去看了一下官網的 document 寫的還蠻詳細的,大家去看看大概就知道我的作法了,底下是我的寫法:
html 部份
<html>
<head>
</head>
<body>
<form name="fileuploadexample2" method="POST" enctype="multipart/form-data" action="">
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<head>
</head>
<body>
<form name="fileuploadexample2" method="POST" enctype="multipart/form-data" action="">
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64"><br />
<input type="file" name="f[]"> 檔案名稱:<input type="text" name="file_show_name[]" value="" size="32" maxlength="64">
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
上傳後 PHP 處理的結果:
<?php
$upload = new HTTP_Upload("en");
$files = $upload->getFiles();
/* 處理自訂檔名 */
$i = "0";
/* file_show_name array number */
foreach($files as $file){
if (PEAR::isError($file)) {
echo $file->getMessage();
}
/* 檔案上傳後 */
if ($file->isValid())
{
$file->setName("uniq");
$moved = $file->moveTo($upload_dir);
if (!PEAR::isError($moved))
{
/* 寫到資料庫裡面 */
$sql = "INSERT INTO " . FILES_TABLE . " (`file_id`, `file_type`, `dateline` , `filesize` , `filename` , `file_real_name`, `file_show_name` , `extention`) VALUES ('".$list_id."', '".$type."', '".$time."', '".$file->getProp("size")."', '".$file->getProp("name")."', '".$file->getProp("real")."', '".$_POST['file_show_name'][$i]."', '".$file->getProp("ext")."')";
if( !($result = $db->sql_query($sql)) )
{
die("Could not query config information " . $sql);
}
$fid = $db->sql_nextid();
}
else {
echo $moved->getMessage();
}
}
elseif ($file->isMissing())
{
}
elseif ($file->isError())
{
echo $file->errorMsg();
}
/* 處理 file_show_name array number */
$i++;
}
?>
$upload = new HTTP_Upload("en");
$files = $upload->getFiles();
/* 處理自訂檔名 */
$i = "0";
/* file_show_name array number */
foreach($files as $file){
if (PEAR::isError($file)) {
echo $file->getMessage();
}
/* 檔案上傳後 */
if ($file->isValid())
{
$file->setName("uniq");
$moved = $file->moveTo($upload_dir);
if (!PEAR::isError($moved))
{
/* 寫到資料庫裡面 */
$sql = "INSERT INTO " . FILES_TABLE . " (`file_id`, `file_type`, `dateline` , `filesize` , `filename` , `file_real_name`, `file_show_name` , `extention`) VALUES ('".$list_id."', '".$type."', '".$time."', '".$file->getProp("size")."', '".$file->getProp("name")."', '".$file->getProp("real")."', '".$_POST['file_show_name'][$i]."', '".$file->getProp("ext")."')";
if( !($result = $db->sql_query($sql)) )
{
die("Could not query config information " . $sql);
}
$fid = $db->sql_nextid();
}
else {
echo $moved->getMessage();
}
}
elseif ($file->isMissing())
{
}
elseif ($file->isError())
{
echo $file->errorMsg();
}
/* 處理 file_show_name array number */
$i++;
}
?>

[...] 最近在實做多重檔案上傳,寫過一篇 [PHP] pear 模組 HTTP_Upload 多重檔案上傳 Multiple files upload,那一開始我先設定只能上傳5個檔案,後來想想動態的話比較方便,畢竟現在網站都講求 web2.0,所以就利用 jQuery 來動態新增 input file 欄位,作法其實很簡單,不難的喔。其實還有 jQuery Confirm Plugin 可以利用它來確定使用者是否刪除檔案。 [...]
[...] .bat 檔,按照視窗,就可以完成安裝了,自己平常有用 Pear 的 HTTP_Upload 多重檔案上傳 Multiple files upload,跟 PEAR – PHP Mail and Mail_Mime 模組,底下就是 Windows 的安裝方法: 1. [...]