我完成了代码,它是这样的:
<?php
ini_set('memory_limit', '128M');
function validate_xml(&$filename)
{
libxml_use_internal_errors(true);
$doc = new DOMDocument('1.0', 'utf-8');
$xml = file_get_contents($filename);
$doc->loadXML($xml);
$errors = libxml_get_errors();
$lines = file($filename);
$output = fopen('errors.txt', 'a');
$distinctErrors = array();
foreach ($errors as $error)
{
if (!array_key_exists($error->line, $distinctErrors))
{
$distinctErrors[$error->line] = $error->message;
fwrite($output, "Filename: {$filename}. Error on line: {$error->line}. {$lines[$error->line-1]}'n");
}
}
fclose($output);
}
if(isset($_POST['SubmitCheck'])) {
$directory = $_POST['Path'];
if ( ! is_dir($directory)) {
exit('Invalid diretory path');
}
else
{
echo "The dir is: $directory". '<br />';
chdir($directory);
foreach (glob("*.xml") as $filename) {
validate_xml($filename);
}
}
}
else {
?>
<form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Path: <input type="text" name="Path"><br>
<input type="hidden" name="SubmitCheck" value="sent">
<input type="Submit" name="Form1_Submit" value="Path">
</form>
<?php
}
?>
它给了我
Notice: Undefined offset: 16032 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16051 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16060 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16075 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16078 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16101 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16110 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
Notice: Undefined offset: 16167 in C:'xampp'htdocs'www'php-xml'nou.php on line 23
问题是我有 3 个大 xml(每个 11MB)。当我在 3 个小 xml 上尝试脚本时,它起作用了。也为每个大的xml单独工作。我不知道该怎么办,我尝试增加php_ini的内存限制......仍然不知道发生了什么。
第 23 行中的代码具有无效的偏移量引用('{$lines[$error->line-1})。你写道:
fwrite($output, "Filename: {$filename}. Error on line: {$error->line}. {$lines[$error->line-1]}'n");
我想你想做:
$line_text = $lines[ $error->line - 1 ];
fwrite($output, "Filename: {$filename}. Error on line: {$error->line}. $line_text 'n");
按照你的方式,PHP 引擎正在解释$error->line-1
,就好像$line-1
是 $error
的属性,而不是算术运算"$line减 1"。