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

效果图

ps://harde.org/blog/wp-content/uploads/2010/04/swfupload01.jpg" />

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'];
 
    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://file.111cn.net/download/2013/05/14/swfupload.zip

时间: 2024-10-21 08:32:58

PHP swfupload图片文件上传实例代码的相关文章

php图片文件上传实例代码

 代码如下 复制代码 <html xmlns="http://www.111cn.net/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>php教程图片文件上传实例代码</title> </head> <body> &

php+ajax实现图片文件上传实例代码

方法一,利用jquery ajaxfileupload.js文件上传 其实就是实现无刷新式的文件上传.可采用IFRAME文件上传原理. 实际上在用PHP上传文件时...只能用$_FILES形式,但是若我们只是单一的用JS方式取其ID,如<input id='img' type='file'>..document.getElementById('img').value或者jquery形式的$("#img")都是不能正真实际上传的(但是还是有很多人这样做,刚开始时我也是). 可

Spring学习笔记2之表单数据验证、文件上传实例代码_java

在上篇文章给大家介绍了Spring学习笔记1之IOC详解尽量使用注解以及java代码,接下来本文重点给大家介绍Spring学习笔记2之表单数据验证.文件上传实例代码,具体内容,请参考本文吧! 一.表单数据验证 用户注册时,需要填写账号.密码.邮箱以及手机号,均为必填项,并且需要符合一定的格式.比如账号需要32位以内,邮箱必须符合邮箱格式,手机号必须为11位号码等.可以采用在注册时验证信息,或者专门写一个工具类用来验证:来看下在SpringMVC中如何通过简单的注释实现表单数据验证. 在javax

php实例多文件上传实例代码

 php实例多文件上传实例代码   //a = $('<span class="MultiFile-title" title="'+MultiFile.STRING.selected.replace('$file', v)+'">'+MultiFile.STRING.file.replace('$file', v.match(/[^/\]+$/gi)[0])+'</span>'),                               

基于html5+java实现大文件上传实例代码_java

废话不多说了,直接给大家贴代码了,具体代码如下所示: html代码如下: <body> <input id="fileid" type="file" accept="video/*;capture=camera" onchange="onfile(this)"> <input id="btn" type="button" value="提交&quo

Java doGet, doPost方法和文件上传实例代码_java

Java doGet, doPost方法和文件上传 index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/test" method="post

PHP 图片文件上传实现代码_php实例

为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的网站源码,全部救变成他的了,直接打包看你的代码.所以一定要控制上传的目录与文件类型,一般只可以上传图片. 创建一个文件上传表单 允许用户从表单上传文件是非常有用的. 请看下面这个供上传文件的 HTML 表单: 复制代码 代码如下: <html> <body> <form action="upload_file.php" method="post" e

node.js实现多图片文件上传实例

先我们来看单文件图片上传 单个的实现我是这样的  代码如下 复制代码 fs.readFile(req.files['file'].path, function(err, data){          fs.writeFile(newPath, data, function(err){                //上传成功           }) }); 如果多文件上传我们只要在此基础上进行修改即可,下面看一个实例. 先看图 这是我当时做多图片的代码,拿出来给大家借鉴一下(有些地方需要亲

php 文件上传实例代码_php实例

项目结构: 上传首页: 上传效果: fileupload.php 复制代码 代码如下: <form action="" enctype="multipart/form-data" method="post" name="uploadfile">上传文件:<input type="file" name="upfile" /><br> <input