php 警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法加载外部实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9771666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:36:19  来源:igfitidea点击:

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

php

提问by Ahmed Abada

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "USD_en_productdata/USD_en_productdata.xml"

警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法加载外部实体“USD_en_productdata/USD_en_productdata.xml”

the code

编码

$src=simplexml_load_file("USD_en_productdata/USD_en_productdata.xml");

foreach($src->ProductItem as $i){
}

回答by Rupesh Patel

Try to pass full directory path if you are trying to load xmls held at your server

如果您尝试加载服务器上保存的 xml,请尝试传递完整目录路径

simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/uproYourFoldet/USD_en_productdat/USD_en_productdata.xml')

or if you want to access xml by http protocol you will need to set allow_url_fopen ON in php.ini or

或者,如果您想通过 http 协议访问 xml,则需要在 php.ini 中设置 allow_url_fopen ON 或

ini_set('allow_url_fopen ','ON');

in your code. or you can also do this if you are using php version <5

在你的代码中。或者,如果您使用的是 php 版本 <5,也可以这样做

$temp = file_get_contents($url);
 $XmlObj = simplexml_load_string($temp); 

回答by Martin Sheeks

Looks like your path might not be correct.

看起来您的路径可能不正确。

In my experience, it's best to surround simplexml_load_file and subsequent functions with if statements with file_exists(...) as the condition.

根据我的经验,最好用 if 语句将 simplexml_load_file 和后续函数括起来,并且以 file_exists(...) 作为条件。

So in your case:

所以在你的情况下:

if(file_exists($_SERVER['DOCUMENT_ROOT'].'/yourPathToFile/...')) {
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/urltoYourFolder/USD_en_productdat/USD_en_productdata.xml')
}

EDIT: spelling

编辑:拼写