PHP用xajax实现无刷新上传

http://tech.ddvip.com   2007年08月26日    社区交流

内容摘要:PHP用xajax实现无刷新上传

  readfirst:

  1.upload files as normal way.Get the upload info from var $_FILES

  2.I use xajax as a lib ,so I don't hack any code but use the extend way,such as reload the class.

  So if someone use this extend,he would not need to change his code a bit.

  3.Working well on (win2k,winXP.sp2)IE 6.0+sp1 / firefox 1.5x,2.x (my OS only have these)

  4.It is kind that let me konw there is any problem of this extend.

  updates:

  2007-3-12 23:26:

  1.fix the FF endless loading problem(thx JensKlose 's tips).

  2.some tips for thoese who want to implement a upload progress meter , check out this link:

  http://www.phpclasses.org/blog/post/61- … -last.html

  There are only 3 files exists(actually,only 2 is necessary).

  1.xajaxExtend.php (server side script, save to the path as xajax.inc.php)

  2.xajax_js/xajax_extend.js (browser side script, save to the path as xajax.js)

  3.tests/uploadTest.php (demo(show you how it work), save to the tests path. Actually I modify this from tests/catchAllFunctionTest.php)

  save to /xajax_0.2.4/xajaxExtend.php

  Code:<?php
/**
* @author RainChen @ Mon Mar 12 23:25:46 CST 2007
* @uses xajax file upload extend
* @access public
* @version 0.1
*/
include_once('xajax.inc.php');
class xajaxExtend extends xajax
{
  
  function processRequests()
  {
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['xajax']))
    {
      $this->initProcessRequests();
    }
    parent::processRequests();
  }
  
  function initProcessRequests()
  {
    $xajaxRequest = array();
    $xajaxRequest['xajaxr'] = @$_GET['xajaxr'];
    $xajaxRequest['xajax'] = @$_GET['xajax'];
    // reset RequestMode
    if(isset($_GET['xajax']))
    {
      $_GET['xajax'] = null;
      unset($_GET['xajax']);
    }
    // get the upload file local path
    foreach(array_keys($_FILES) as $name)
    {
      if(isset($_GET[$name]) && !isset($_POST[$name]))
      {
        $_POST[$name] = $this->_decodeUTF8Data($_GET[$name]);
      }
    }
    $xajaxargs = array($this->getOriginalData($_POST));
    $xajaxRequest['xajaxargs'] = $xajaxargs;
    $_POST = $xajaxRequest;
  }
  
  function getJavascript($sJsURI="", $sJsFile=NULL)
  {  
    $html = parent::getJavascript($sJsURI,$sJsFile);
    // get the extend js
    if ($sJsFile == NULL) $sJsFile = "xajax_js/xajax_extend.js";
    if ($sJsURI != "" && substr($sJsURI, -1) != "/") $sJsURI .= "/";
    $html .= "  <script type="text/javascript" src="" . $sJsURI . $sJsFile . ""></script>
";
    return $html;
  }
  
  /**
   * get original request data from GET POST
  **/
  function getOriginalData($data)
  {
    if($data)
    {
      if(get_magic_quotes_gpc())
      {
        if (is_array($data))
        {
          foreach($data as $key=>$value)
          {
            $data[$key] = xajaxExtend::getOriginalData($value);
          }
        }
        else
        {
          $data = stripslashes($data);
        }
      }
    }
    return $data;
  }
}
?>

作者:RainChen    责编:豆豆技术应用

正在加载评论...