swfupload是一個flash插件它可以結合php來快速實現圖片文件無刷新上傳,同時還可以批量上傳圖片,下面我來給大家介紹PHP swfupload圖片文件上傳實例代碼有需要了解的中參考。
效果圖
index.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>
<title>SWFUpload Demos - SWFObject Demo</title>
<link href="../css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/swfupload.swfobject.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 swfu;
SWFUpload.onload = function () {
var settings = {
flash_url : "../swfupload/swfupload.swf",
upload_url: "upload.php",
post_params: {
"PHPSESSID" : "NONE",
"HELLO-WORLD" : "Here I Am",
".what" : "OKAY"
},
file_size_limit : "100 MB",
file_types : "*.jpg;*.gif;*.png;*.jpeg;",
file_types_description : "All Files",
file_upload_limit : 100,
file_queue_limit : 0,
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
debug: false,
// Button Settings
button_image_url : "XPButtonUploadText_61x22.png",
button_placeholder_id : "spanButtonPlaceholder",
button_width: 61,
button_height: 22,
// The event handler functions are defined in handlers.js
swfupload_loaded_handler : swfUploadLoaded,
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,
queue_complete_handler : queueComplete, // Queue plugin event
// SWFObject settings
minimum_flash_version : "9.0.28",
swfupload_pre_load_handler : swfUploadPreLoad,
swfupload_load_failed_handler : swfUploadLoadFailed
};
swfu = new SWFUpload(settings);
}
</script>
</head>
<body>
<div id="header">
<h1 id="logo"><a href="../">SWFUpload</a></h1>
<div id="version">v2.2.0</div>
</div>
<div id="content">
<h2>SWFObject Demo</h2>
<form id="form1" action="index.php" method="post" enctype="multipart/form-data">
<p> This page demonstrates the SWFObject plugin. Do each of the following (one at a time) to see the plugin work: </p>
<ul>
<li>Uninstall your Flash Player or install a version less than 9.0.28</li>
<li>Cause the SWF file to fail to load by deleting or renaming swfupload.swf (simulating a very slow or failed download)</li>
<li>Disable JavaScript</li>
</ul>
<p>
Each of these tests demontrate how these issues can be handled by SWFUpload and the SWFObject libraries.
</p>
<div id="divSWFUploadUI">
<div class="fieldset flash" id="fsUploadProgress">
<span class="legend">Upload Queue</span>
</div>
<p id="divStatus">0 Files Uploaded</p>
<p>
<span id="spanButtonPlaceholder"></span>
<input id="btnCancel" type="button" value="Cancel All Uploads" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</p>
</div>
<noscript>
<div style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px;">
We're sorry. SWFUpload could not load. You must have JavaScript enabled to enjoy SWFUpload.
</div>
</noscript>
<div id="divLoadingContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
SWFUpload is loading. Please wait a moment...
</div>
<div id="divLongLoading" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
SWFUpload is taking a long time to load or the load has failed. Please make sure that the Flash Plugin is enabled and that a working version of the Adobe Flash Player is installed.
</div>
<div id="divAlternateContent" class="content" style="background-color: #FFFF66; border-top: solid 4px #FF9966; border-bottom: solid 4px #FF9966; margin: 10px 25px; padding: 10px 15px; display: none;">
</div>
</form>
</div>
</body>
</html>
核心處理程序php代碼
if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
$upload_file = $_FILES['Filedata'];
$file_info = pathinfo($upload_file['name']);
$file_type = $file_info['extension'];
$save = 'image/' . md5(uniqid($_FILES["Filedata"]['name'])) . '.' . $file_info['extension'];
$name = $_FILES['Filedata']['tmp_name'];
if (!move_uploaded_file($name, $save)) {
exit;
}
//將數組的輸出存起來以供查看
$fileName = 'test.txt';
$postData = var_export($file_info, true);
$file = fopen('' . $fileName, "w");
fwrite($file,$postData);
fclose($file);
}
swfUpload注意事項
swfuplaod在上傳時,會新開一個進程,和原來的進程不一致,要解決這個問題,需要指定session_id,然後在登錄頁面判斷,如果有post過來的session_id,那麼就用函數session_id( $_POST['PHP_SESSIONID'])指定一下。
上傳頁的JS裡面,可以獲取當前的SESSION_ID的。
例如上傳頁的JS中:
代碼如下 復制代碼post_params: {"PHPSESSID": "<?php echo session_id(); ?>"},
在驗證的判斷頁中:
代碼如下 復制代碼 if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
(這一段是網上的注釋:在帶有Session驗證的網站後台中SWFUpload無法正常工作,這是因為SWFUpload在上傳時相當於重新開辟了一個新的Session 進程,因此無法與原有程序的Session保持一致,這就需要在上傳時傳遞原有程序的SessionID,根據它來“找回”其應有的Session。)
完整實例下載地址:http://file.php100.com/download/2013/05/14/swfupload.zip