PHPExcel_IOFactory :: load($ target_file)正在使用localhost但不在服务器上

时间:2015-12-07 05:59:01

标签: php jquery excel

将excel文件导入服务器时,它将无效或不会出现任何错误。

这里,究竟发生了什么,首先在导入excel文件时首先我将该文件上传到服务器然后我使用该文件将该数据导入数据库。 文件已正确上传到服务器,但随后无法继续工作。 我已调试该文件并了解

PHPExcel_IOFactory::load($target_file); is not working

请帮忙。谢谢

代码在这里:

 <!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        function closeWin() {
            myWindow.close();
        }
    </script>
</head>
<body>
    <?php
    session_start();

    function GetFileExtension($imagetype) {
        if (empty($imagetype))
            return false;
        switch ($imagetype) {
            case 'xlsx': return '.xlsx';
            case 'xls': return '.xls';
            default: return false;
        }
    }

    libxml_use_internal_errors(true);
//include the following 2 files
    include './config.php';
    require '../../PHPExcel_1.8.0/Classes/PHPExcel.php';
    require_once '../../PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
    $target_dir = './uploads/';
    $product = filter_input(INPUT_POST, 'product');
    $username = $_SESSION['pman'];
    $target_file = $target_dir . basename($_FILES["userfile"]["name"]);
    $filename = basename($_FILES["userfile"]["name"]);
    $file_type = pathinfo($filename, PATHINFO_EXTENSION);
    $Type_status = true;
    $ext = GetFileExtension($file_type);
    if ($ext == FALSE) {
        echo 'This File Type is not Supported';
        echo 'Please use Excel sheets with extension *.xlsx or *.xls';
        echo '<a id="click" href="javascript:window.close()">Close this     window</a>';
    } else {
        if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $target_file)) {
            echo 'File Uploaded';
        } else {
            echo 'error'.  mysql_error();
        }
        $objPHPExcel = PHPExcel_IOFactory::load($target_file);
        foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
            $worksheetTitle = $worksheet->getTitle();
            $highestRow = $worksheet->getHighestRow(); // e.g. 10
            $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
            if ($highestColumn != 'L') {
                echo 'This File is as per given Standered';
                echo 'Please Check Template for perticular standards';
                echo '<a id="click"   href="javascript:window.close()">Close this window</a>';
                break;
            }
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $nrColumns = ord($highestColumn) - 64;
            echo "<br>The worksheet " . $worksheetTitle . " has ";
            echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
            echo ' and ' . $highestRow . ' row.';
            echo '<a id="click" href="javascript:window.close()">Close this window</a>';
            echo '<br>Data: <table border="1"><tr>';
            for ($row = 1; $row <= $highestRow; ++$row) {
                $valarray = array();
                echo '<tr>';
                for ($col = 0; $col < $highestColumnIndex; ++$col) {
                    $cell = $worksheet->getCellByColumnAndRow($col, $row);
                    $valarray[] = $cell->getValue();
                    $dataType = PHPExcel_Cell_DataType::dataTypeForValue($valarray[$col]);
                    echo '<td>' . $valarray[$col] . '</td>';
                }
                if ($row > 1) 
                {
                    if (strtolower($valarray[2]) == 'prepaid' || strtolower($valarray[2]) == 'postpaid') {
                        if ($valarray[0] == "") {
                            $Type_status = false;
                        } else {

                        }
                    } else {
                        $Type_status = false;
                    }
                }

                echo '</tr>';
            }
            echo '</table>';
        }
        $count = 0;
        if ($Type_status == true) {
            if ($highestColumn != 'L') {

            } else {
                $upd = null;
                $cmt = null;
                $rev = 'Pending';
                for ($row = 2; $row <= $highestRow; ++$row) {
                    $val = array();
                    for ($col = 0; $col < $highestColumnIndex; ++$col) {
                        $cell = $worksheet->getCellByColumnAndRow($col, $row);
                        $val[] = $cell->getValue();

                    }
                    $testcaseid = '1234' . rand(50, 5000);

                    $sql_insert = $conn->prepare("INSERT INTO tmtool.testcase_master (`Testcase_id`,`Testcase_title`, `Precondition`, `Product_id`, `Subscriber_type`, `Created_by`, `Activation_mode`, `Testcase_desc`, `Date_created`, `Test_step`, `Expected_result`, `Category`, `Sub_category`,`Language`,`Priority`, `Updated_date`, `Comment`, `Review`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                    $sql_insert->bind_param('ssssssssssssssssss', $testcaseid, $val[0], $val[1], $product, $val[2], $username, $val[3], $val[4], $val[5], $val[6], $val[7], $val[8], $val[9], $val[10], $val[11], $upd, $cmt, $rev);
                    if ($sql_insert->execute()) {
                        $sql_insert->close();
                        $count++;
                    } else {
                        die(mysqli_error($conn));
                    }

                }
                echo $count . 'Test cases has been Imported to the Database';
                echo '<a id="click" href="javascript:window.close()">Close this window</a>';
            }
        } else {
            echo 'Some Testcase dont have Subscriber Type or Testcase Title ...It is a Mandatory Field. Can not be Empty';
            echo '<a id="click" href="javascript:window.close()">Close this window</a>';
        }
    }
    ?>
</body>

2 个答案:

答案 0 :(得分:0)

这可能是文件大小的问题。如果文件太大,PHPExcel可能无法读取您的文件。这是一个清除疑虑的好地方:https://phpexcel.codeplex.com/workitem/list/basic

答案 1 :(得分:0)

我发现了问题。问题在于我的库。我使用过phpexcel库版本1.8.0。但仍有一些更新发生在单词之后。

我过去使用的是没有正确使用.xlsx文件。但是可以使用.xls。

我遇到的问题是:当我上传.xlsx文件时,它会自动将其转换为.xls。而且无法处理。 解决方案:我已经更新了我的PHPExcel库,因此它对.xlsx和.xls都很好。