> 백엔드 개발 > PHP 튜토리얼 > PHP swfupload图片文件上传范例代码

PHP swfupload图片文件上传范例代码

WBOY
풀어 주다: 2016-06-13 12:20:07
원래의
886명이 탐색했습니다.

PHP swfupload图片文件上传实例代码

swfupload是一个flash插件它可以结合php来快速实现图片文件无刷新上传,同时还可以批量上传图片,下面我来给大家介绍PHP swfupload图片文件上传实例代码有需要了解的中参考。

效果图



?

index.php文件

<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><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 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>
로그인 후 복사

?

核心处理程序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。)

DEMO下载地址:http://dl.iteye.com/topics/download/b6351ce4-b3e3-30a3-b8af-eda7b98a1c79

?

?

?

?

?

?

?

?

?

?

?

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿