檔案上傳之SWFUpload外掛程式(代碼)_php執行個體

來源:互聯網
上載者:User
下面通過一段代碼給大家示範下,主要分為1.前台檔案index.html和 2.後台檔案upload.php。具體代碼如下所示:

1.前台檔案index.html SWFUpload 

SWFUpload

v2.2.0 Hanization By Leo.C,

2.後台檔案upload.php

 <?php // 傳遞session值(由於Flash與session不相容,只能通過參數傳遞擷取) if (isset($_POST["PHPSESSID"])) {  session_id($_POST["PHPSESSID"]); } else if (isset($_GET["PHPSESSID"])) {  session_id($_GET["PHPSESSID"]); }  session_start(); // 設定POST最大值 $POST_MAX_SIZE = ini_get('post_max_size'); $unit = strtoupper(substr($POST_MAX_SIZE, -1)); $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));  if ((int)$_SERVER['CONTENT_LENGTH'] > $multiplier*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) {  header("HTTP/1.1 500 Internal Server Error");  echo "POST exceeded maximum allowed size.";  exit(0); } // 基本設定 $save_path = getcwd() . "/file/";                // 檔案上傳位置 $upload_name = "Filedata"; $max_file_size_in_bytes = 2147483647;             // 2GB $extension_whitelist = array("doc", "txt", "jpg", "gif", "png"); // 允許檔案類型 $valid_chars_regex = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';   // 檔案名稱規則 // 其他變數 $MAX_FILENAME_LENGTH = 260; $file_name = ""; $file_extension = ""; $uploadErrors = array(  0=>"檔案上傳成功",  1=>"上傳的檔案超過了 php.ini 檔案中的 upload_max_filesize directive 裡的設定",  2=>"上傳的檔案超過了 HTML form 檔案中的 MAX_FILE_SIZE directive 裡的設定",  3=>"上傳的檔案僅為部分檔案",  4=>"沒有檔案上傳",  6=>"缺少臨時檔案夾" ); // 檢測檔案是否上傳正確 if (!isset($_FILES[$upload_name])) {  HandleError("No upload found in \$_FILES for " . $upload_name);  exit(0); } else if (isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0) {  HandleError($uploadErrors[$_FILES[$upload_name]["error"]]);  exit(0); } else if (!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"])) {  HandleError("Upload failed is_uploaded_file test.");  exit(0); } else if (!isset($_FILES[$upload_name]['name'])) {  HandleError("File has no name.");  exit(0); } // 檢測檔案尺寸 $file_size = @filesize($_FILES[$upload_name]["tmp_name"]); if (!$file_size || $file_size > $max_file_size_in_bytes) {  HandleError("File exceeds the maximum allowed size");  exit(0); }  if ($file_size <= 0) {  HandleError("File size outside allowed lower bound");  exit(0); } // 檢測檔案名稱字為空白 $file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', "", basename($_FILES[$upload_name]['name'])); if (strlen($file_name) == 0 || strlen($file_name) > $MAX_FILENAME_LENGTH) {  HandleError("Invalid file name");  exit(0); } // 檢測重名檔案 if (file_exists($save_path . $file_name)) {  HandleError("File with this name already exists");  exit(0); } // 檢測尾碼名 $path_info = pathinfo($_FILES[$upload_name]['name']); $file_extension = $path_info["extension"]; $is_valid_extension = false; foreach ($extension_whitelist as $extension) {  if (strcasecmp($file_extension, $extension) == 0) {   $is_valid_extension = true;   break;  } } if (!$is_valid_extension) {  HandleError("Invalid file extension");  exit(0); } // 儲存檔案 if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path.$file_name)) {  HandleError("檔案無法儲存.");  exit(0); } // 成功輸出  echo "File Received"; exit(0); function HandleError($message) { header("HTTP/1.1 500 Internal Server Error"); echo $message;}?>

以上代碼就是實現檔案上傳之SwFUpload外掛程式的全部內容,希望大家喜歡。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.