自用PHP在线Access转html表格小功能(支持大文件上传,得到一表一文件)
本工具用于Access数据表转html下载供预览,特别适合各类程序员。
开发环境(可能只支持)Windows IIS PHP5.4 其他系统或新版本都不支持
<?php
/*
//自用PHP在线Access转html表格小功能(支持大文件上传,得到一表一文件)
//本工具用于Access数据表转html下载供预览,特别适合各类程序员。
//作者信息:yujianyue<15058593138@qq.com> 请保留版权信息
//开发环境(可能只支持)Windows IIS PHP5.4 其他系统或新版本都不支持
//不定期更新详情关注:https://Access2html.96448.cn/
*//*
广告:易名一口价出售珍藏域名
网址:https://135634.shop.ename.com/
域名 简介 类型 注册时间
yichabiao.cn 易查表(格) 易名一口价 2020/4/20
maLide.cn 码立得 易名一口价 2020/4/22
echafen.cn 易名一口价 2016/8/29
11170.cn 5数客服米 易名一口价 2012/9/5
aiyaha.com 哎呀哈 易名一口价 2014/10/9
mabida.com 易名一口价 2015/4/5
saoLide.cn 扫(码)立得 易名一口价 2021/4/21
Lidehui.cn 立得惠 易名一口价 2021/6/4
96448.cn 谐音:就来试试吧 易名一口价 2011/9/28
souLide.cn 搜立得 易名一口价 2020/4/9
*///error_reporting(0);
$biaoti = "自用PHP在线Access转html表格小功能(Access2html)";
$dlimi = "1"; //分片大小MB
$zlimi = "10"; //总文件限制MB
$upext = ".mdb|.asa";//上传格式限定;|隔开
$upexe = str_replace("|",",",$upext);//上传格式限定
$tima = date("YmdHis");
//以下只为获得功能页面网址用于上传文件网址拼接
$iport = $_SERVER['SERVER_PORT'];
$indes = $_SERVER['SCRIPT_NAME'];
$inder = dirname($_SERVER['PHP_SELF']);
$ihost = $_SERVER['HTTP_HOST'];
if(stristr("-443-","-$iport-")){
$ihosts = "https://$ihost{$inder}";
}elseif(stristr("-80-","-$iport-")){
$ihosts = "http://$ihost{$inder}";
}else{
$ihosts = "http://$ihost:{$iport}{$inder}";
}
/*
免费查询系统下载: http://96448.cn
php+mysql系列查询系统 http://sql.chalide.cn
php+Txt系列查询系统 http://txt.chalide.cn
一站式万用查询平台1:http://chalide.com
一站式万用查询平台2:http://add.12391.net
*/
$css = "<meta charset=\"utf-8\" />";
$css .= "<style>\r\n *{margin:0; padding:0; font-size:12px;}";
$css .= "table{margin:5px auto;border-left:1px solid #a2c6d3;border-top:3px solid #0180CF;width:96vw;}";
$css .= "table td{border-right:1px solid #a2c6d3;border-bottom:1px solid #a2c6d3;}";
$css .= "table td{padding:5px;word-wrap:break-word;word-break:break-all;max-width:300px;}";
$css .= ".tt{background:#e5f2fa;line-height:150%;font-size:14px;padding:5px 9px;}";
$css .= "</style>";
function jsonMsg($code,$msg,$url=''){$arr = []; $arr['code'] = $code; $arr['msg'] = $msg; $arr['url'] = $url;header('Content-type: application/json');exit(json_encode($arr));
}
function heFile($filebath,$bnum) {$wfp = fopen($filebath, $bnum == 1 ? "wb" : "ab");$rfp = fopen($_FILES['file']['tmp_name'], "rb");$bool = false;while ($buff = fread($rfp, 4096)) {if(!$bool = fwrite($wfp, $buff)) return $bool;}fclose($wfp);fclose($rfp);return $bool;
}
function characet($data){if(!empty($data) ){ $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ; if( $fileType != 'UTF-8'){ $data = mb_convert_encoding($data ,'utf-8' , $fileType); } } return $data;
}
//如果含html则替换<>'"
function reps($stext){
$stext = str_replace(array("<", ">",), array("<", ">"), $stext);
$stext = str_replace(array("\r\n","\r","\n"),"<br />",$stext);
return characet($stext);
}
function db2zip($zipfile,$dbsname,$tima){
global $css;
if(!file_exists($dbsname)){ exit("存储路径出错!"); }$shuoy = "<h1>$dbsname</h1>"; $zip = new ZipArchive;
$res = $zip->open($zipfile, ZipArchive::CREATE);
if ($res === TRUE) {
//当前读取无密码数据文件
$cstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ={$dbsname}";
$conn = @odbc_connect($cstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!");
$result = odbc_tables($conn); $table=[];
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE") $table[] =odbc_result($result,"TABLE_NAME");
}odbc_close($conn);if(count($table)<1){ exit("读取失败:空白数据表!");}
foreach($table as $rowname){$rawhtml = showbiao($dbsname,$rowname);$zip->addFromString("$rowname.html", "$css".$rawhtml);$shuoy .= "<p><a href=\"$rowname.html\" target='_blank'>$rowname</a></p>\r\n";
}
//$zip->addFile("chalide.zip","chalide.zip");
$zip->addFromString("_ReadMe.txt", $shuoy);
$zip->close();
} else {
exit("fail zip:ZipArchive failure");
}
}
function showbiao($dbsname,$rewname){
$cstr = "DRIVER=Microsoft Access Driver (*.mdb);DBQ={$dbsname}";
$conn = @odbc_connect($cstr,"","",SQL_CUR_USE_ODBC ) or die ("数据库连接错误!");
$query = @odbc_do($conn, "select * from [{$rewname}]");
$fieldcount = odbc_num_fields($query);
$h .= "<table cellspacing=\"0\" class=\"table\"> \r\n";
$h .= "<caption>表:$rewname ({$fieldcount}列)</caption>";
$h .= '<tr class="tt">';
for( $i=0; $i<$fieldcount; $i++){
$info = reps(odbc_field_name($query,$i+1));
$h .= "<td><nobr>$info</nobr></td>";
}
$h .= '</tr>';
while ($row = odbc_fetch_row($query)){
$iy++; $h .= '<tr>';
for( $i=0; $i<$fieldcount; $i++){
$info = reps(odbc_result($query,$i+1));
$h .= "<td>$info</td>";
}
$h .= '</tr>';
}
if($iy<1){ $h .= "<tr><td colspan={$i}>没有查询到相关信息哦</td></tr>"; }
$h .= '</table>';
odbc_close($conn);
return $h;
}if($_FILES['file']['tmp_name'] && $_POST['blob_num']){$filex = $_FILES['file'];$filen = (isset($_POST['file_name']))?Trim($_POST['file_name']):'';$timen = (isset($_POST['nowYmd']))?Trim($_POST['nowYmd']):date("YmdHis").$uniqid();
if ($filex["error"] > 0){
$fer = []; $ercode = $filex["error"];
$fer["1"] = '上传的文件超过了upload_max_filesize选项限制的值';
$fer["2"] = '上传文件的大小超过了表单MAX_FILE_SIZE选项指定的值';
$fer["3"] = '上传异常,文件只有部分被上传';
$fer["4"] = '文件空白或者说没有文件被上传';
$fer["6"] = '上传的临时文件丢失';
$fer["7"] = '文件写入失败建议检查文件夹读写权限';
if($fer[$ercode]){
jsonMsg("0","Error 00".$ercode.":".$fer[$ercode]);
}else{
jsonMsg("0","Error 00".$ercode.":未知上传错误!");
}
}else{
$info = pathinfo($filen); $tape = strtolower($info['extension']);
if(!stristr("|$upext|","|.{$tape}|")){ jsonMsg("0","Error 010,后缀.{$tape}不支持!");}
if($filex["size"]>$dlimi*1024*1024){ jsonMsg("0","Error 011,单片文件大小超{$dlimi}MB!");}
if($_POST["filesize"]>$zlimi*1024*1024){ jsonMsg("0","Error 012,文件总大小超{$zlimi}MB!");}
$filepath = "./upload/"; //存储路径
if(!is_dir($filepath)) { @mkdir($filepath,0755,true);}
if(!is_dir($filepath)) { jsonMsg("0","Error 015,文件夹失败{$filepath}!");}
$filebath = $filepath . "$timen.$tape";
$bnum = (isset($_POST['blob_num']))?addslashes($_POST['blob_num']):'';
$tnum = (isset($_POST['total_blob_num']))?addslashes($_POST['total_blob_num']):'';
$fnum = (isset($_POST['filesize']))?addslashes($_POST['filesize']):'0';
if(!is_numeric($bnum) || !is_numeric($tnum)) jsonMsg("0","Error 0161,分片参数得数字!");
if(!is_numeric($fnum) ) jsonMsg("0","Error 0162,参数filesize得数字!");$urlx = $ihosts.Ltrim($filebath,".").".zip";
if("@$bnum" == "@1" && "@$tnum"=="@1"){move_uploaded_file($_FILES['file']['tmp_name'],$filebath);//直接更名保存$fsize=filesize($filebath);$fzip = db2zip($filebath.".zip",$filebath,$tima);if($fnum==$fsize) jsonMsg("2","OK",$urlx); else jsonMsg("2","可能异常!",$urlx);
}elseif($bnum<$tnum && $tnum>1){heFile($filebath,$bnum);jsonMsg("1","waiting $bnum/$tnum !");
}elseif($bnum==$tnum && $tnum>1){heFile($filebath,$bnum);$fsize=filesize($filebath);$fzip = db2zip($filebath.".zip",$filebath,$tima);if($fnum==$fsize) jsonMsg("2","OK@$filebath",$urlx); else jsonMsg("2","可能异常!",$urlx);
}else{jsonMsg("0","Error 017,分片异常:$bnum / $tnum!");
}
}
}?>
<!DOCTYPE html>
<html>
<head><title><?php echo "$biaoti"; ?></title><style type="text/css">body {font-family: Arial, sans-serif;font-size: 14px;background-color: #f2f2f2;}h3{margin: 0;font-size: 18px;line-height: 1.8;color: blue;}p {margin: 0; font-size: 14px; line-height: 1.5;}#urls {color: blue;}.container {margin: 50px auto;padding: 20px 20px 50px 20px;background-color: #fff;border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,0.3);max-width: 775px;}#mask{position: fixed; top: 0; left: 0; width: 100%; height: 100%;background-color: rgba(0,0,0,0.5); display: none;}#dialog{position: fixed; top: 50%; left: 50%;min-width: 300px; transform: translate(-50%,-50%);background-color: #fff; padding: 20px; border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,0.5); display: none;}#dialog .btn, .bts{display: flex; float: left; padding: 5px 8px;background-color: #007bff; color: #fff; border-radius: 3px;cursor: pointer; margin: 10px 10px 10px 0;}#dialog .btn:hover, .bts:hover{background-color: #0069d9;}#progress{width: 100%; min-width: 300px; height: 20px;background-color: #f7f7f7;box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);border-radius: 4px;background-image: linear-gradient(to bottom, #f5f5f5, #f9f9f9);}.finish {background-color: #149bdf;background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);background-size: 20px 20px;height: 100%;}</style>
</head>
<body>
<div class="container"><h3><?php echo "$biaoti"; ?></h3><p>自用在线Access转html表格小功能(支持大文件上传,得到一表一文件)</p><p>说明:限制大小<?php echo $zlimi;?>Mb;支持<?php echo $upexe?>格式上传!</p><?php echo '<p>作用:上传Access数据得到各表html表格数据!</p>'; ?><?php echo '<p>环境:可能仅支持WIN PHP5.4/Linux不支持/高版本PHP不支持!</p>'; ?><div style="margin-bottom:20px;display:block;"><button onclick="up('file')" class="bts">上传</button></div><input type="file" id="file" accept="<?php echo $upexe?>" style="display:none;">
</div><div id="mask"></div><div id="dialog"><div id="progress"><div id="uend" class="finish" style="width: 0%;" progress="0"></div></div><div id="back"></div><div id="buts"><button class="btn" id="ok" onclick="hideDialog()">确定</button></div></div><script type="text/javascript">
function $(Obj){return document.getElementById(Obj);}
function up(Obj){ $(Obj).value=""; $(Obj).click();}var mask = $('mask');function showDialog(){mask.style.display = 'block';dialog.style.display = 'block';}function hideDialog(){mask.style.display = 'none';dialog.style.display = 'none';}var tici = Date.parse(new Date())/1000;var ufile = $("file");var jdt_s = $('uend');var ipt_s = $('back');ufile.onchange = function () {tici = tici + 1; $("back").innerHTML="";jdt_s.style.width = "1%"; jdt_s.innerHTML = "";showDialog();var vupload = new Upload(jdt_s, ipt_s);//后移解决二次选择文件上传失败vupload.addFileAndSend(this);};function Upload(jdt, ipt) {const LENGTH = 1024 * 1024 * <?php echo $dlimi;?>;//分片上传每片数量MBvar start = 0; var end = start + LENGTH;var blob; var blob_num = 1;this.addFileAndSend = function (that) {var file = that.files[0];
if(!file){ ipt_s.innerHTML = "<b>未上传:</b>未选择文件!"; return false;}
if (!/(<?php echo $upext;?>)$/.test(file.name)){ipt_s.innerHTML = '<b>未上传:</b>仅支持后缀格式(<?php echo $upexe;?>)!'; return false;
}
if (file.size > <?php echo $zlimi;?> * 1024 * 1024){ipt_s.innerHTML = '<b>未上传:</b>文件大小超<?php echo $zlimi;?>MB限制!'; return false;
}blob = cutFile(file);sendFile(blob, file);blob_num += 1;};function cutFile(file) {var file_blob = file.slice(start, end);start = end; end = start + LENGTH;return file_blob;}function sendFile(blob, file) {var xhr = new XMLHttpRequest();var fdata = new FormData();var total_blob_num = Math.ceil(file.size / LENGTH);fdata.append('file', blob);fdata.append('blob_num', blob_num);fdata.append('filesize', file.size);fdata.append('nowYmd', tici);fdata.append('total_blob_num', total_blob_num);fdata.append('file_name', file.name);xhr.open('POST', '?act=up&t='+Math.random(), false);xhr.onreadystatechange = function () {var progress; var progressObj = jdt;if (total_blob_num === 1) {progress = '100%';} else {progress = Math.ceil((blob_num / total_blob_num) * 10000) / 100 + '%';}progressObj.style.width = progress;progressObj.innerHTML = progress;var tt = setTimeout(function () {var jj = JSON.parse(xhr.response);console.log(jj);if (jj.code == 2 || jj.code == 1) {if (start < file.size) {blob = cutFile(file);sendFile(blob, file);blob_num += 1;}
if (jj.code == 2){ $("back").innerHTML='<h3>上传成功:</h3>下载:<a href="'+jj.url+'" target="_blank">'+jj.url+'</a>';}}
if (jj.code == 0){ $("back").innerHTML='<h3>上传失败:</h3><b>'+jj.msg+'</b>';}xhr = undefined;fdata = undefined;}, 500);};xhr.send(fdata);}}
</script></body>
</html>