我完成了代码,它是这样的:
<?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 的内存限制......仍然不知道发生了什么。