在ThinkPHP框架下用AjaxFileUploader插件实现ajax文件上传

在ThinkPHP框架下用AjaxFileUploader插件实现ajax文件上传,支持多种文件格式,页面无刷新上传。

在Lib/Action/目录下创建upAction.class.php文件,代码如下:

<!--?php
class upAction extends BaseAction{

public function index(){
	$this--->display();
}

/*
*@文件上传
*@author	FineYi
*@date		2013-01-23
*/
public function upLoadFile(){
	$error = "";
	$msg = "";
	$fileElementName = 'fileToUpload';
	if(!empty($_FILES[$fileElementName]['error'])){
		switch($_FILES[$fileElementName]['error']){
			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = 'No file was uploaded.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none'){
		$error = 'No file was uploaded..';
	}else{
			$re = $this-&gt;up();
			if(!$re){
				$error = 'Up file fail';
			}
			$msg = $re['savename'];	//文件名
			$path = '/upload/bizcoop/'.$msg;	//文件路径
			$size = $re['size'];	//文件大小
	}		
	echo json_encode(array('error'=&gt;$error,'msg'=&gt;$msg,'path'=&gt;$path,'size'=&gt;$size));exit;
}

private function up(){ 
	import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下
	$upload=new UploadFile();

	$upload-&gt;maxSize='-1';//默认为-1,不限制上传大小
	$upload-&gt;savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';//保存路径
	$upload-&gt;saveRule=uniqid;//上传文件的文件名保存规则
	$upload-&gt;uploadReplace=true;//如果存在同名文件是否进行覆盖
	$upload-&gt;allowExts=array('jpg','jpeg','png','gif');//准许上传的文件类型
	if($upload-&gt;upload()){
		$info=$upload-&gt;getUploadFileInfo();
		return $info[0];
	}else{
		return false;
		exit;
	} 
}

}
?&gt;

在/Tpl/default/Up/目录下创建index.tpl文件,代码如下:

&lt;div id="content"&gt;
&lt;h1&gt;Ajax File Upload Demo&lt;/h1&gt;
&lt;img id="loading" style="display: none;" alt="" src="__APP____PUBLIC__/style/img/loading.gif" /&gt;

&lt;form action="" enctype="multipart/form-data" method="POST" name="form"&gt;
&lt;table class="tableForm" cellspacing="0" cellpadding="0"&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input class="input" id="fileToUpload" type="file" name="fileToUpload" size="45" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;button class="button" id="buttonUpload" onclick="return ajaxFileUpload();"&gt;Upload&lt;/button&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;span&gt;已上传的附件:&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tfoot&gt;&lt;/tfoot&gt;
&lt;/table&gt;
&lt;/form&gt;&lt;/div&gt;

在/Lib/Org/目录下放入ThinkPHP文件上传类:
下载ThinkPHP扩展包 下载
解压缩  类文件路径  /Extend/Library/ORG/Net/UploadFile.class.php

上传中loading文件:loading

demo上传效果:
0307201135
Jquery Ajax File Uploader插件下载地址 下载