利用一个js插件实现, 主要阐述一下前台部署代码和后台封装
JS插件下载链接:点击打开链接
前台JS调用代码
ajaxfileupload({
url : url,
secureuri : false,
fileElementId : 'excel_orders_file',
dataType : 'json',
success : function(json, status) {
if (!json.success) {
alert(json.msg);
} else {
alert('导入成功!'+json.msg);
}
},
});
后台文件上传类封装:
<?php
require_once WEB_PATH.'include/util/TmpFile.cls.php';
/**
* @author jixiaolong
* @todo AjaxFileUpload插件配套的PHP类
*
*/
class AjaxFileUpload {
private $uploadFile;
private $tmpCopy;
function __construct($fileElementName) {
$this->tmpCopy = null;
if (!array_key_exists($fileElementName, $_FILES)){
$message = "没有找到fileElementName为{$fileElementName}的上传文件";
throw new Exception($message, 0);
}
$this->uploadFile = $_FILES[$fileElementName];
if ($this->hasUploadError()) {
$error = $this->getUploadErrorMsg();
throw new Exception($error, 0);
}
else if (!$this->hasUploadFile()) {
$error = 'No file was uploaded..';
throw new Exception($error, 0);
}
else{
;
}
}
/**
*
*/
function __destruct() {
$this->delFromTmpDir();
}
/**
* 判断是否上传了文件
*/
private function hasUploadFile(){
return !(empty($this->uploadFile['tmp_name']) || $this->uploadFile['tmp_name'] == 'none');
}
/**
* 判断是否有上传错误
*/
private function hasUploadError(){
return !empty($this->uploadFile['error']);
}
/**
* 获取上传错误信息
*/
private function getUploadErrorMsg(){
switch ($this->uploadFile ['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';
}
return $error;
}
/**
* 获取上传文件信息
*/
public function getFileInfo(){
return $this->uploadFile;
}
/**
* 获取一个临时文件夹中的副本
*/
public function getATmpCopy(){
if (empty($this->tmpCopy)){
$this->copyFileToTmpDir();
}
return $this->tmpCopy;
}
/**
* 从临时文件夹中删除副本
*/
private function delFromTmpDir(){
$this->tmpCopy->delFile();
$this->tmpCopy = null;
}
/**
* 将文件拷贝到一个以原文件扩展名命名的临时文件
*/
private function copyFileToTmpDir(){
if (!empty($this->tmpCopy)){
//如果已经有了,删除
$this->delFromTmpDir();
}
$orignalName = $this->getOrignalName();
$pathinfo = pathinfo($orignalName);
$extendName = $pathinfo['extension'];
$newName = date('YmdHis').'.'.$extendName;
$newPath = WEB_PATH.'tmp_file/'.$newName;
$tmpPath = $this->getTmpFilePath();
copy($tmpPath, $newPath);
$TmpFile = new TmpFile();
$TmpFile->setPath($newPath);
$TmpFile->setName($orignalName);
$TmpFile->setType($this->getFileType());
$TmpFile->setTmpName($newName);
$this->setTmpCopyFile($TmpFile);
}
private function setTmpCopyFile(TmpFile $TmpFile){
$this->tmpCopy = $TmpFile;
}
/**
* 获取原始文件名
*/
private function getOrignalName(){
return $this->uploadFile['name'];
}
private function getFileType(){
return $this->uploadFile['type'];
}
private function getTmpFilePath(){
return $this->uploadFile['tmp_name'];
}
private function getError(){
return $this->uploadFile['error'];
}
private function getFileSize(){
return $this->uploadFile['size'];
}
}
服务器处理接收示例:
require_once WEB_PATH . 'include/util/Result.cls.php';
require_once WEB_PATH . 'include/util/AjaxFileUpload.php';
$Result = new Result ( true );
try {
//转存到本地临时文件
$UploadFile = new AjaxFileUpload ( 'excel_orders_file' );
$TmpFile = $UploadFile->getATmpCopy ();
$TmpFile->setSysClass ( TypeFileClass::TMP_FILE );
$TmpFile->setAddTime ( date ( 'Y-m-d H:i:s' ) );
$TmpFile->setAddUser ( $this->user ['uid'] );
$ExcelReader = new ExcelReader ( $TmpFile->getPath (), 'Sheet1' );
$max = 10000;
for($i = 2; $i < $max; $i ++) {
//跳过第一行表头,从第二行开始处理
$orderId = $ExcelReader->getCellContent ( 'A', $i );
$marketAmount = $ExcelReader->getCellContent ( 'B', $i );
$orderId = intval ( trim ( $orderId ) );
$marketAmount = doubleval(trim($marketAmount));
if (empty($orderId)){
break;
}
}
} catch ( Exception $e ) {
$Result->setSuccess ( false );
$Result->setMsg ( $e->getMessage () );
$Result->setCode ( $e->getCode () );
}
echo $Result->toJSON ();
return;
附录:
临时文件类TmpFile封装:
<?php
require_once ('include/util/File.cls.php');
/**
* @author jixiaolong
*
*
*/
class TmpFile extends File {
private $tmpName;
/**
*
*/
public function __construct() {
parent::__construct ();
}
/**
*
*/
function __destruct() {
}
/**
* @return the $tmpName
*/
public function getTmpName() {
return $this->tmpName;
}
/**
* @param field_type $tmpName
*/
public function setTmpName($tmpName) {
$this->tmpName = $tmpName;
}
}
文件类File封装:
<?php
/**
* @author jixiaolong
* 文件处理类
*
*/
require_once WEB_PATH . 'core/dao/FileDao.cls.php';
require_once WEB_PATH.'include/util/FileInterface.cls.php';
class File implements FileInterface{
protected $name;
protected $type;
protected $addTime;
protected $addUser;
protected $sysClass;
protected $path;
protected $fileDao;
function __construct() {
$this->name = '';
$this->type = '';
$this->addTime = '0000-00-00 00:00:00';
$this->addUser = 0;
$this->sysClass = 0;
$this->path = '';
$this->fileDao = new FileDao ();
}
/**
*
*/
function __destruct() {
}
/**
* @return the $name
*/
public function getName() {
return $this->name;
}
/**
* @return the $type
*/
public function getType() {
return $this->type;
}
/**
* @return the $addTime
*/
public function getAddTime() {
return $this->addTime;
}
/**
* @return the $addUser
*/
public function getAddUser() {
return $this->addUser;
}
/**
* @return the $sysClass
*/
public function getSysClass() {
return $this->sysClass;
}
/**
* @return the $path
*/
public function getPath() {
return $this->path;
}
/**
* @param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
/**
* @param field_type $type
*/
public function setType($type) {
$this->type = $type;
}
/**
* @param field_type $addTime
*/
public function setAddTime($addTime) {
$this->addTime = $addTime;
}
/**
* @param field_type $addUser
*/
public function setAddUser($addUser) {
$this->addUser = $addUser;
}
/**
* @param field_type $sysClass
*/
public function setSysClass($sysClass) {
$this->sysClass = $sysClass;
}
/**
* @param field_type $path
*/
public function setPath($path) {
$this->path = $path;
}
public function addDBRecord() {
$arr = array (
'sys_class' => $this->sysClass,
'name' => $this->name,
'type' => $this->type,
'path' => $this->path,
'add_time' => $this->addTime,
'add_user' => $this->addUser
);
$this->fileDao->doAdd ( $arr );
}
public function delFile(){
if (!empty($this->path)){
$res = preg_match('/^http/', $this->path);
if (empty($res)){
@unlink($this->path);
}
}
}
/**
* 获取文件链接
* @return string html形式的文件链接
*/
public function getLink(){
return "<a href='{$this->getPath()}'>{$this->getName()}</a>";
}
}
文件接口封装:
<?php
interface FileInterface {
public function getName();
public function getType();
public function getAddTime();
public function getAddUser();
public function getSysClass();
public function getPath();
public function setName($name);
public function setType($type);
public function setAddTime($addTime);
public function setAddUser($addUser);
public function setSysClass($sysClass);
public function setPath($path);
public function addDBRecord();
public function delFile();
}