文件上传之SWFUpload插件(代码)

这篇文章主要介绍了文件上传之SWFUpload插件(代码),实现此代码主要分为两部分:1.前台文件index.html和 2.后台文件upload.php,需要的朋友可以参考下。

下面通过一段代码给大家演示下,主要分为1.前台文件index.html和 2.后台文件upload.php,具体代码如下所示:

1.前台文件index.html

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

2.后台文件upload.php

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

以上代码就是实现文件上传之SwFUpload插件的全部内容,希望大家喜欢。