如何使用PHP验证Excel文件? [英] How to validate Excel File using PHP?

查看:85
本文介绍了如何使用PHP验证Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

如何验证excel文件(.xls和.xlsx)?



我的PHP代码中有一个上传功能,但是我没有办法验证excel文件是.xls还是.xlsx。



以下是示例代码:



Hi!
How do I validate excel file(.xls and .xlsx)?

I have an upload function in my PHP code but I do not have a way to validate if excel file is .xls or .xlsx.

Here is the sample code:

if(isset($_POST['Submit'])){
    $duplicatedRows = "";
    $mimes = array('application/vnd.ms-excel','text/xls','text/xlsx');

    set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
    include 'PHPExcel/IOFactory.php';

    if(in_array($_FILES["csv"]["type"],$mimes)){
        // This is the file path to be uploaded.
        $inputFileName = $_FILES["csv"]["tmp_name"];

        try {
            $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }


        $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet


        for($i=2;$i<=$arrayCount;$i++){
            $item = trim($allDataInSheet[$i]["A"]);
            $category = trim($allDataInSheet[$i]["B"]);
            $model = trim($allDataInSheet[$i]["C"]);
            $serialnum = trim($allDataInSheet[$i]["D"]);
            $description = trim($allDataInSheet[$i]["E"]);
            $barcode = trim($allDataInSheet[$i]["F"]);
            $satellite = trim($allDataInSheet[$i]["G"]);
            $location = trim($allDataInSheet[$i]["H"]);
            $remarks = trim($allDataInSheet[$i]["I"]);
            $department = trim($allDataInSheet[$i]["J"]);
            $supplier = trim($allDataInSheet[$i]["K"]);
            $accntble = trim($allDataInSheet[$i]["L"]);
            $cost = trim($allDataInSheet[$i]["M"]);
            $lifespan = trim($allDataInSheet[$i]["N"]);
            $movable = trim($allDataInSheet[$i]["O"]);
            $datepurchased = trim($allDataInSheet[$i]["P"]);

            $result = mysql_query("select * from tblItem");
            $count = mysql_num_rows($result);
            $dateToday = date("ymd");
            $itemNum = $dateToday.$count;

            //get date today for CreatedDate
            $createdDate = date('Y-m-d H:i:s');

            //check if their is duplicate in Serial Number and Barcode. It also check if date is valid.
            $duplicateList = checkFields($serialnum, $barcode);


            $query = "INSERT INTO `tblitem`(`ItemID`, `Item`, `Category`, `Model`, `SerialNum`, `Description`, `Barcode`, `Satellite`
            , `Location`, `Remarks`, `Supplier`, `Department`, `Accntble`, `Cost`, `LifeSpan`, `Movable`
            , `DatePurchased`, `CreatedBy`, `CreatedDate`) VALUES
            ('".$itemNum."','".$item."','".$category."','".$model."','".$serialnum."','".$description."','".$barcode."','".$satellite."','".$location."'
            ,'".$remarks."','".$supplier."','".$department."','".$accntble."','".$cost."'
            ,'".$lifespan."','".$movable."','".$datepurchased."','20140','".$dateToday."');";
            //$sql = mysql_query($query);
            //$recResult = mysql_fetch_array($sql);
        s
            if($duplicateList=="")
            {
                $insertTable= mysql_query($query);

                //$msg = 'Records has been added.';
            }
            else { $duplicatedRows .= "<li>Row ".$rowCount. $duplicateList ."</li>"; }
        }
        //echo "<div style='font: bold 18px arial,verdana;padding: 45px 0 0 500px;'>".$msg."</div>";

        if ($duplicatedRows <> ""){
            //echo $duplicateList."<br/>";
            echo "<br/>Item rows that has not been uploaded. <ul> ".$duplicatedRows . "</ul>";//list of duplicate not uploaded.
            echo "Some items are already uploaded. [".$arrayCount." Items]";
        }
        else{ echo "All items from CSV are already uploaded. [".$arrayCount." Items]"; }
    } else { die("<br/>Sorry, File type is not allowed. Only Excel file."); }
}





我有这个验证器,但它无法正常工作,因为当我尝试上传.csv文件时,它接受它会导致我的代码出错。



感谢您的帮助。



I have this validator but it is not working properly because when I try to upload a .csv file, it accepts it which causes error in my codes.

Thank you for the help.

推荐答案

_POST [' 提交'])){
_POST['Submit'])){


duplicatedRows = ;
duplicatedRows = "";


mimes = array(' application / vnd.ms-excel'' text / xls' ' text / xlsx');

set_include_path(get_include_path()。PATH_SEPARATOR。' Classes /');
包括' PHPExcel / IOFactory.php';

if(in_array(
mimes = array('application/vnd.ms-excel','text/xls','text/xlsx'); set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); include 'PHPExcel/IOFactory.php'; if(in_array(


这篇关于如何使用PHP验证Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆