国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

文件上傳之SWFUpload插件(代碼)

2024-05-04 23:38:26
字體:
供稿:網(wǎng)友

這篇文章主要介紹了文件上傳之SWFUpload插件(代碼),實(shí)現(xiàn)此代碼主要分為兩部分:1.前臺文件index.html和 2.后臺文件upload.php,需要的朋友可以參考下

下面通過一段代碼給大家演示下,主要分為1.前臺文件index.html和 2.后臺文件upload.php。具體代碼如下所示:

 

 
  1. 1.前臺文件index.html 
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  3. <html xmlns="http://www.w3.org/1999/xhtml" > 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <head> 
  6. <title>SWFUpload</title> 
  7. <link href="css/default.css" rel="stylesheet" type="text/css" /> 
  8. <!--Swfupload插件begin--> 
  9. <script type="text/javascript" src="swfupload/swfupload.js"></script> 
  10. <script type="text/javascript" src="js/swfupload.queue.js"></script> 
  11. <script type="text/javascript" src="js/fileprogress.js"></script> 
  12. <script type="text/javascript" src="js/handlers.js"></script> 
  13. <!--Swfupload插件end--> 
  14. <script type="text/javascript"
  15. var swfu; 
  16. window.onload = function() { 
  17. var settings = { 
  18. flash_url : "swfupload/swfupload.swf"
  19. upload_url: "upload.php"// 后臺文件 
  20. post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"}, 
  21. file_size_limit : "100 MB"
  22. file_types : "*.*"
  23. file_types_description : "All Files"
  24. file_upload_limit : 100, 
  25. file_queue_limit : 0, 
  26. custom_settings : { 
  27. progressTarget : "fsUploadProgress"
  28. cancelButtonId : "btnCancel" 
  29. }, 
  30. debug: false
  31. // 按鈕設(shè)置 
  32. button_image_url: "images/TestImageNoText_65x29.png"// Flash樣式圖片文件 
  33. button_width: "65"
  34. button_height: "29"
  35. button_placeholder_id: "spanButtonPlaceHolder"
  36. button_text: '<span class="theFont">瀏覽</span>'
  37. button_text_style: ".theFont { font-size: 16; }"
  38. button_text_left_padding: 12, 
  39. button_text_top_padding: 3, 
  40. // 句柄設(shè)置 
  41. file_queued_handler : fileQueued, 
  42. file_queue_error_handler : fileQueueError, 
  43. file_dialog_complete_handler : fileDialogComplete, 
  44. upload_start_handler : uploadStart, 
  45. upload_progress_handler : uploadProgress, 
  46. upload_error_handler : uploadError, 
  47. upload_success_handler : uploadSuccess, 
  48. upload_complete_handler : uploadComplete, 
  49. queue_complete_handler : queueComplete 
  50. }; 
  51. swfu = new SWFUpload(settings); 
  52. }; 
  53. </script> 
  54. </head> 
  55. <body> 
  56. <div id="header"
  57. <h1 id="logo"><a href="/">SWFUpload</a></h1> 
  58. <div id="version">v2.2.0</div> 
  59. </div> 
  60. <div id="content"
  61. <form id="form1" action="index.php" method="post" enctype="multipart/form-data"
  62. <p>點(diǎn)擊“瀏覽”按鈕,選擇您要上傳的文檔文件后,系統(tǒng)將自動上傳并在完成后提示您。</p> 
  63. <p>請勿上傳包含中文文件名的文件!</p> 
  64. <div class="fieldset flash" id="fsUploadProgress"
  65. <span class="legend">快速上傳</span> 
  66. </div> 
  67. <div id="divStatus">0 個(gè)文件已上傳</div> 
  68. <div> 
  69. <span id="spanButtonPlaceHolder"></span> 
  70. <input id="btnCancel" type="button" value="取消所有上傳" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" /> 
  71. </div> 
  72. </form> 
  73. </div> 
  74. <div align="center">Hanization By <a href="http://imll.net" target="_blank">Leo.C,</a> 
  75. </div> 
  76. </body> 
  77. </html> 

2.后臺文件upload.php

 

 
  1. <?php 
  2. // 傳遞session值(由于Flash與session不兼容,只能通過參數(shù)傳遞獲取) 
  3. if (isset($_POST["PHPSESSID"])) { 
  4. session_id($_POST["PHPSESSID"]); 
  5. else if (isset($_GET["PHPSESSID"])) { 
  6. session_id($_GET["PHPSESSID"]); 
  7. session_start(); 
  8. // 設(shè)置POST最大值 
  9. $POST_MAX_SIZE = ini_get('post_max_size'); 
  10. $unit = strtoupper(substr($POST_MAX_SIZE, -1)); 
  11. $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1))); 
  12. if ((int)$_SERVER['CONTENT_LENGTH'] > $multiplier*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) { 
  13. header("HTTP/1.1 500 Internal Server Error"); 
  14. echo "POST exceeded maximum allowed size."
  15. exit(0); 
  16. // 基本設(shè)置 
  17. $save_path = getcwd() . "/file/";             // 文件上傳位置 
  18. $upload_name = "Filedata"
  19. $max_file_size_in_bytes = 2147483647;          // 2GB 
  20. $extension_whitelist = array("doc""txt""jpg""gif""png"); // 允許文件類型 
  21. $valid_chars_regex = '.A-Z0-9_ !@#$%^&()+={}/[/]/',~`-'// 文件名規(guī)則 
  22. // 其他變量 
  23. $MAX_FILENAME_LENGTH = 260; 
  24. $file_name = ""
  25. $file_extension = ""
  26. $uploadErrors = array( 
  27. 0=>"文件上傳成功"
  28. 1=>"上傳的文件超過了 php.ini 文件中的 upload_max_filesize directive 里的設(shè)置"
  29. 2=>"上傳的文件超過了 HTML form 文件中的 MAX_FILE_SIZE directive 里的設(shè)置"
  30. 3=>"上傳的文件僅為部分文件"
  31. 4=>"沒有文件上傳"
  32. 6=>"缺少臨時(shí)文件夾" 
  33. ); 
  34. // 檢測文件是否上傳正確 
  35. if (!isset($_FILES[$upload_name])) { 
  36. HandleError("No upload found in /$_FILES for " . $upload_name); 
  37. exit(0); 
  38. else if (isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0) { 
  39. HandleError($uploadErrors[$_FILES[$upload_name]["error"]]); 
  40. exit(0); 
  41. else if (!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"])) { 
  42. HandleError("Upload failed is_uploaded_file test."); 
  43. exit(0); 
  44. else if (!isset($_FILES[$upload_name]['name'])) { 
  45. HandleError("File has no name."); 
  46. exit(0); 
  47. // 檢測文件尺寸 
  48. $file_size = @filesize($_FILES[$upload_name]["tmp_name"]); 
  49. if (!$file_size || $file_size > $max_file_size_in_bytes) { 
  50. HandleError("File exceeds the maximum allowed size"); 
  51. exit(0); 
  52. if ($file_size <= 0) { 
  53. HandleError("File size outside allowed lower bound"); 
  54. exit(0); 
  55. // 檢測文件名字為空 
  56. $file_name = preg_replace('/[^'.$valid_chars_regex.']|/.+$/i'"", basename($_FILES[$upload_name]['name'])); 
  57. if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH) { 
  58. HandleError("Invalid file name"); 
  59. exit(0); 
  60. // 檢測重名文件 
  61. if (file_exists($save_path . $file_name)) { 
  62. HandleError("File with this name already exists"); 
  63. exit(0); 
  64. // 檢測后綴名 
  65. $path_info = pathinfo($_FILES[$upload_name]['name']); 
  66. $file_extension = $path_info["extension"]; 
  67. $is_valid_extension = false
  68. foreach ($extension_whitelist as $extension) { 
  69. if (strcasecmp($file_extension, $extension) == 0) { 
  70. $is_valid_extension = true
  71. break
  72. if (!$is_valid_extension) { 
  73. HandleError("Invalid file extension"); 
  74. exit(0); 
  75. // 保存文件 
  76. if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) { 
  77. HandleError("文件無法保存."); 
  78. exit(0); 
  79. // 成功輸出 
  80. echo "File Received"
  81. exit(0); 
  82. function HandleError($message) { 
  83. header("HTTP/1.1 500 Internal Server Error"); 
  84. echo $message; 
  85. ?> 

以上代碼就是實(shí)現(xiàn)文件上傳之SwFUpload插件的全部內(nèi)容,希望大家喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 湟源县| 建阳市| 临猗县| 赤城县| 麻栗坡县| 瑞金市| 杭锦旗| 长白| 平远县| 长宁区| 小金县| 商南县| 舟曲县| 平山县| 大连市| 大庆市| 拜城县| 景德镇市| 正镶白旗| 苗栗县| 正定县| 海城市| 万安县| 云和县| 资阳市| 阿坝| 建水县| 启东市| 辉县市| 三亚市| 大方县| 甘泉县| 赫章县| 兰考县| 临沂市| 游戏| 宁远县| 铜川市| 舒城县| 咸宁市| 彭阳县|