PHPExcel_IOFactory::load($target_file) 在本地主机上工作,但不在服务器上工作

标签 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>

最佳答案

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

关于PHPExcel_IOFactory::load($target_file) 在本地主机上工作,但不在服务器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34127361/

相关文章:

php - 清理没有可用干净版本的被黑网站的最佳方法?

php - 数组到字符串转换注意事项。为什么?

php - 如何使用 PHP 更新数据库中的特定记录?

javascript - 是否可以在没有 jQuery 的情况下使用 Materializecss?

excel - VBA 拼写检查,突出显示拼写错误的单词的单元格

vba - Excel/VBA -- 基于 abritrary col/row 中的 int 在下面创建行

php - PHP 变量的全局访问

javascript - 使用 jQuery 编码元素

javascript - 在 Codepen 中检测 CSS-only 更新

java excel 到 csv 文件转换