【PHP】SWFUpload多檔上傳處理

摘要:【PHP】SWFUpload多檔上傳處理

 

由於案子需求,開始study我不是很熟悉的檔案上傳部份,而且又要大量檔案可以批次上傳!!

 

可以不要一開始就讓我頭有點痛嗎...

 

發現了有幾套,大約看過之後,我還是比較喜歡與FLASH整合的SWFUpload,操作感覺也很簡約順暢

 

我就是愛有時尚感的東東XD

 

不過,很多文章都交代得不是很清楚,連官方文件都是含含糊糊

 

 

官方Demo頁 http://demo.swfupload.org/v220/index.htm

 

 

於是就著手更改他的範例,來弄成我要的東西

 

index.php

 

<?php
session_start();
 
if (count($_FILES)) {
        // Handle degraded form uploads here.  Degraded form uploads are POSTed to index.php.  SWFUpload uploads
// are POSTed to upload.php
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SWFUpload測試</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/swfupload.queue.js"></script>
<script type="text/javascript" src="js/fileprogress.js"></script>
<script type="text/javascript" src="js/handlers.js"></script>
<script type="text/javascript">
var upload1;
 
window.onload = function() {
upload1 = new SWFUpload({
// Backend Settings
upload_url: "upload.php",  <-- 處理上傳動作
post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
 
// File Upload Settings
file_size_limit : "10240",  <--檔案上傳大小限制 // 10MB
file_types : "*.*",  <-- 上傳檔案類型
file_types_description : "All Files",
file_upload_limit : "50",  <--  一次上傳數量限制
file_queue_limit : "0",
 
// Event Handler Settings (all my handlers are in the Handler.js file)
file_dialog_start_handler : fileDialogStart,
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_start_handler : uploadStart,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
 
// Button Settings
button_image_url : "js/XPButtonUploadText_61x22.png",  <-- 按鈕圖片路徑
button_text:    ,    <--   也可以用文字表示,可以用CSS增加樣式
button_placeholder_id : "spanButtonPlaceholder1",   <--  取代成按鈕的元素ID
button_width: 61,    <---  寬
button_height: 22,  <---  高
 
// Flash Settings
flash_url : "js/swfupload/swfupload.swf",
 
 
custom_settings : {
progressTarget : "fsUploadProgress1",
cancelButtonId : "btnCancel1"
},
 
// Debug Settings
debug: false  <--  Debug模式開啟與關閉
});
 
    }
</script>
</head>
 
<body>
<div id="content">
<h2>多檔上傳</h2>
<form id="form1" action="index.php" method="post" enctype="multipart/form-data">
<p>
</p>
<table>
<tr valign="top">
<td>
<div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder1"></span>
<input id="btnCancel1" type="button" value="Cancel Uploads" onclick="cancelQueue(upload1);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</div>
<div class="fieldset flash" id="fsUploadProgress1">
 <span class="legend">檔案上傳進度</span>
</div>
</div>
</td>
 </tr>
 </table>
  </form>
</div>
</body>
</html>

 

 

upload.php

<?php
 
//設置session
if (isset($_POST["PHPSESSID"])) {
 session_id($_POST["PHPSESSID"]);
 }
 session_start(); 
 
 //做個簡易的錯誤判斷,顯示於Debug中
 if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
 echo "上傳時出現錯誤 - 無法取得檔名。";
 } 
 
//FILES參數
 $img = $_FILES["Filedata"]["tmp_name"];
 $imgname = substr(md5(time().$_FILES["Filedata"]["name"]),0,16).$_FILES["Filedata"]["name"];
 $path = './images/'.$imgname;
 
//
 if(@move_uploaded_file($img, $path))
 echo '<a href="http://'.$_SERVER['HTTP_HOST'].'/weine/images/'.$imgname.'" target="_blank">'.$_FILES['Filedata']['name'].'</a><BR>上傳成功!';
 else
 echo '圖片上傳發生錯誤 - 無法複製檔案。';
 
?>