PHP 并不总能找到 XML 文件

标签 php xml linux apache

我四处寻找类似的问题,但没有发现相同的问题,所以这里是:

我在 linux/apache 服务器上有这个网页,PHP 脚本在每次执行时都会打开一个 XML 文件,有趣的是有时它可以读取该文件,而在极少数情况下它不能..当它无法读取它说该文件不存在并且要修复它我只需要刷新页面。

XML 文件可以在子目录中找到(例如 /srv/www/page/subdir/file.xml)并且只能手动更改并设置正确的权限(PHP 可以读取和写入它)。

我正在使用 simplexml,我得到的错误是:I/O warning : failed to load external entity

所以服务器中发生了一些奇怪的事情,我只是想不通是什么..

编辑:添加一些代码

if ( !file_exists( $file ) )
{           
    error_log("File " . $file . " does not exist!");
    //return FALSE;
}

if ( $obj = simplexml_load_file( $file ) )
{
    // do stuff
    return TRUE;
}
else
{
    error_log("Could not parse XML: " . $file );
    return FALSE;
}

已解决:代码错误,而非服务器错误

最佳答案

没有明显的解释为什么文件偶尔不可用。可能是平台/操作系统问题,很难说。

但是,您可以使用此创可贴解决问题,如果等待最多 3 秒使文件“重新出现”,它可能会解除对您的阻止:

$count = 0;
$found = FALSE;
while( $count < 3 && !$found ) {
   if ( !file_exists( $file ) ) {
      $count++;
      sleep( 1 ); // sleep 1 second to see if the file appears on the next try
   } else
      $found=TRUE;
}

if( !$found )
  die("file not found after 3 attempts");

关于PHP 并不总能找到 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471641/

相关文章:

php - 如何使用复选框在 mySQL 中输入 true (1) 或 false (0) 并在 php/html 表单中显示为选中状态?

java - 分类层次结构表示格式

c - C语言从内存中读取地址

php - laravel 中 3 个相关表的数据

php - Pimcore 安装页面空白

c# - 比较 2 个 XML 文档并将更改应用到源文档

c - printf 如何知道何时传递有符号 int

linux - 如何将 C 运行时库与 'ld' 链接起来?

php - Prestashop模块调试

java - XMLDecoder >> java.lang.IllegalArgumentException : Unsupported element