【问题标题】:PHP import failedPHP 导入失败
【发布时间】: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


    【解决方案1】:

    我喜欢你目前所做的。预测用户行为的前瞻性非常好。

    我建议使用 jquery 进度条。

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

    当然,您还需要“onwindow close”检测,以便您可以弹出“确认”对话框。

    $(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']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2018-03-17
      • 2020-03-04
      • 2014-03-28
      相关资源
      最近更新 更多