我完成了代码,就像这样:
<?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个大xmls(每个11MB)。 当我尝试3个小xmls上的脚本时,它工作。还为每个大xml单独工作。 我不知道该做什么,我试着增加php_ini的内存限制......仍然不知道发生了什么。
答案 0 :(得分:0)
第23行中的代码具有无效的偏移引用(`{$ lines [$ error-&gt; 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”。