有问题的脚本采用excel文件词汇表(法语到英语等)并创建XML(拉链和下载)以格式化我们使用的填字游戏生成器。
它有效,但我已被要求删除任何重复的条款作为增强功能。下面是完整的原始代码,然后是跳过重复术语的新代码。使用新代码,一切都运行,但它会创建一个损坏的ZIP。请查看前后代码并告诉我发生了什么:
在使用代码之前完整:
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
/** Functions */
require_once 'zip.php';
require_once 'named_to_number.php';
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
/** Create Excel Object using PHPExcel **/
$inputFileName = $_FILES["file"]["tmp_name"];
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();
/** Get how many rows **/
$highestRow = $objWorksheet->getHighestRow();
/** Keeps track of chapters to make new files on change **/
$chapter = -1;
/** For keeping track of the files created to zip & delete. **/
$files_to_zip = array();
/** Iterates through every row, writing data from cells into XML files. **/
for ($row = 2; $row <= $highestRow; ++$row) {
//remove spaces
$term = str_replace(' ', '', $objWorksheet->getCellByColumnAndRow(1, $row)->getValue());
//skip terms if they are too long or contain a non-alpha character.
if (strlen($term) >= 17 || !preg_match('/^\p{L}+$/ui', $term)) {
continue;
}
//translates accented characters to numbered HTML code
$term = named_to_number(htmlentities($term, ENT_SUBSTITUTE , 'UTF-8'));
/** Checks first column to see if the chapter has changed.
If it has, the current file will be closed and a new one opened. **/
if ($chapter != $objWorksheet->getCellByColumnAndRow(0, $row)->getValue()){
fwrite ($f, "</words>\n</content>");
fclose($f);
if (strlen($objWorksheet->getCellByColumnAndRow(0, $row)->getValue()) < 2) {
$filename = 'ch0' . $objWorksheet->getCellByColumnAndRow(0, $row)->getValue() . '.xml';
} else {
$filename = 'ch' . $objWorksheet->getCellByColumnAndRow(0, $row)->getValue() . '.xml';
}
$f = fopen($filename, 'a');
/** Add to the list of files to zip and delete **/
array_push($files_to_zip, $filename);
fwrite ($f, "<content>\n<words>\n");
/** Update chapter value **/
$chapter = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
}
/** Write terms **/
$data =
"<word><entry>" . $term .
"</entry><clue>" . $objWorksheet->getCellByColumnAndRow(2, $row)->getValue() .
"</clue></word>\n";
fwrite ($f, $data);
}
fwrite ($f, "</words>\n</content>");
fclose($f);
/** Removes any blank ch.xml files **/
if(($key = array_search('ch.xml', $files_to_zip)) !== false) {
unset($files_to_zip[$key]);
unlink('chapter.htm');
}
$zip = create_zip($files_to_zip, 'crossword.zip');
foreach($files_to_zip as &$del){
unlink($del);
}
header("Content-disposition: attachment; filename=crossword.zip");
header("Content-type: application/zip");
readfile("crossword.zip");
unlink("crossword.zip");
?>
添加代码的相关代码段(仅与新代码相关的评论):
//array to be used to hold terms to check against
$used = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$term = str_replace(' ', '', $objWorksheet->getCellByColumnAndRow(1, $row)->getValue());
//Add term to running array to check against to see if it exists
array_push($used, $term);
//Added a third condition to check the array to see if the term exists. I have also tried this using isset with array_flip.
if (strlen($term) >= 17 || !preg_match('/^\p{L}+$/ui', $term) || in_array($term, $used)) {
continue;
}
同样,奇怪的是脚本运行,但它只会产生一个坏的zip。肯定是第三个条件是绊倒脚本。我已经在自己的if语句中尝试过它(只是为了确保语法正确),但问题仍然存在。
请帮忙!
谢谢,
麦克