【发布时间】:2016-07-14 18:22:54
【问题描述】:
我创建了一个 PHP 上传页面,以便将 xls 文件从 pc 上传到 MySQL 表,除了两个问题外,整个过程根据需要运行得很好。 假设正在上传的用户不小心关闭了浏览器,或者点击了另一个链接;在这种情况下,部分项目被上传到 MySQL 表,其余的都失败了。如何强制用户等待上传完成,或者创建一个确认框,如果它返回 false,它将删除已上传的项目。
还有一个问题 - 有没有办法在提交后禁用上传按钮,我搜索了谷歌并找到了很多解决方案,但它们都不适用于 Chrome 浏览器。
如果你想要这里是我上传的PHP代码:
提交表单后显示Loader gif
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script>
<!--Loader-->
function getVal(){
$('#load').load('loader.htm');
}
<!--End of Loader-->
</script>
上传文件
<form enctype="multipart/form-data" method="post" >
<table width="100%" height="%" align="center" border="1" cellpadding="4" cellspacing="0">
<tr><td align="center" colspan="2"><img src="images/import.png" width="108" height="108"></td></tr>
<tr>
<td><input type="file" name="file" id="file"></td></tr>
<tr >
<td colspan="2" align="center" height="70px"><br /><br /><input type="submit" onClick="getVal()" name="Import_items" value="Import Items" style="background-color:orange; font-weight:bold; font-size:16px; width:200px; height:50px;"></td>
</tr>
</table>
</form>
<center>
<div id="load"></div>
</center>
提交后
if(isset($_POST["Import_items"]))
{
$filename=$_FILES["file"]["tmp_name"];
if(empty($filename)){
?>
<script>
alert('Error : Please Select Excel File to Import !');
location.replace('import.php');
</script>
<?php
exit();
}
//Check File Extension if Valid or Not (Excel Only Format)
$allowed = array('csv','xls','xlsx');
$ff = $_FILES['file']['name'];
$ext = pathinfo($ff, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo "<center>Extension is Not Excel !</center>";
?>
<script>
alert('Error : Please Upload Correct Excel File Extension');
location.replace('import.php');
</script>
<?php
exit();
}
$date = date('Y-m-d');
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileName = $filename; // File to read
//echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$num = count($sheetData);
for($i=2;$i<=$num;$i++){
if($sheetData[$i]['B'] ==0 && $sheetData[$i]['C'] ==0){
Something 1
}
else {
//Something 2
if(!empty($sheetData[$i]['E']) && (!empty($sheetData[$i]['E']))){
//Do Something
}
else{
// Do something Else
}
}
}
}
//For Loop end
}
谢谢大家,任何帮助将不胜感激!
【问题讨论】:
标签: php mysql forms upload submit