-
PHP swfupload图片文件上传实例代码
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'];
- //开源代码phpfensi.com
- 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://www.phpfensi.com/php/20140827/4849.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式