PHP导入失败


PHP import failed

我创建了一个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
}

谢谢各位,如有任何帮助,不胜感激!

我喜欢你迄今为止所做的工作。非常好的前瞻性思维,能够预测用户的行动。

我建议使用jquery进度条。

http://www.sitepoint.com/html5-javascript-file-upload-progress-bar/

当然,你还需要一个"窗口关闭"检测,这样你就可以弹出一个"确认"对话框。

$(window).unload(function() {
   //do something
}

那么您就应该拥有自己的功能。

在服务器端保持干净。只要成功或失败就可以了。

除此之外。。。。真的很好:)


在后端尝试这个:

class Uploader
{
$responses = [
    'Error : Please Upload Correct Excel File Extension',
    'Error : Please Select Excel File to Import !',
    'Success: Your file has been processed & uploaded.'
];
function returnResponses($response)
{
    return sprintf("<script>alert('%s');
location.replace('import.php');
</script>", $responses[$response]);
}
function checkUpload($file)
{
    $filename=$file["tmp_name"];
    if(isset($_POST["Import_items"]))
    {
        if(empty($filename))
        {
            return $this->returnResponses(1);
        }
    }
    else
    {
        $allowed =  array('csv','xls','xlsx');
        $ff = $file['name'];
        $ext = pathinfo($ff, PATHINFO_EXTENSION);
        if(!in_array($ext,$allowed) ) 
        {
            return "<center>Extension is Not Excel !</center>" . $this->returnResponses(0);
        }
        else{
            //success & process files
            $date = date('Y-m-d');
            //process your file
            return $this->returnResponses(2);
        }
    }
}
}
$uploader = new Uploader();
return $uploader->checkUpload($_FILES['file']);