PHP Warning: DOMDocument::load(): I/O warning : failed to load external entity
Asked Answered
P

3

9

I read everything I could about this error without being able to find any solution.

I have a simple page that looks like this:

$xmlfile = "/var/www/marees.xml"; //Fichier dans lequel récupérer les données
$ent = new DOMDocument();
$ent->load($xmlfile);

if(!(@$ent->load($xmlfile)))
{
    echo "Unable to load : " . $xmlfile;
    exit();
}

I get three times out of four, randomly this error:

PHP Warning: DOMDocument::load(): I/O warning : failed to load external entity "/var/www/marees.xml" in /var/www/marees/test2.php on line 7

When I restart Apache, the script works fine for 5 minutes, then the error starts to appear.

XML file weighs 595 kB, is present and readable.

What might be the problem?

Ponton answered 6/5, 2014 at 8:39 Comment(5)
What does var_dump(file_exists($xmlfile)); return?Wellgroomed
try : chmod 777 /var/www/marees.xmlMetalanguage
I've tried chmod 777, same result (the file was already readable/writable by www-data) the var_dump(file_exists($xmlfile)); return always : bool(true) (even if there is an error in the load xmlfile or not)Ponton
Do you have Another Idea ? :-)Ponton
Most often if you enable error logging and raise error reporting to the highest level, you find more information about that error in your error log. Take that information and contact your server administrator to discuss the issue.Hobgoblin
V
10

add this command to the top of your script:

libxml_disable_entity_loader(false);

For more details see this link.

Vermicular answered 12/6, 2014 at 17:14 Comment(1)
Is there an alternative solution for PHP 8+ as this method has been deprecated? See php.net/manual/en/function.libxml-disable-entity-loader.phpDuncan
A
1
public mixed DOMDocument::load ( string $filename [, int $options = 0 ] )

The function declaration carries with it an optional parameter named $options where:

options
Bitwise OR of the libxml option constants.

The usage of the LIBXML_NOWARNING constant solves the issue for me:

$ent->load($xmlfile, LIBXML_NOWARNING);
Apnea answered 9/6, 2016 at 10:8 Comment(0)
A
0

In my case, I was copying some files in the root directory of XML. And this process was changing permission of the root directory recursively. So I just changed the permission using below code after copying files and it worked.

        mkdir($rootDirectoryAddress, 0777, true);
Arrington answered 27/5, 2022 at 9:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.