1

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

我在 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;
}

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

4

1 回答 1

1

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

但是,您可以将此创可贴应用于该问题,如果等待最多 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");
于 2012-05-06T15:40:49.757 回答