douren7179 2012-05-06 15:15
浏览 31

PHP并不总能找到XML文件

I looked around for similar questions but found none identical, so here goes:

I have this webpage on a linux/apache server, the PHP script opens a XML-file upon every execution, the funny thing is that sometimes it can read the file and on some rare occasions it can not.. And when it cannot read it says that the file does not exist and to fix it I just have to refresh the page.

The XML file can be found in a sub directory (e.g. /srv/www/page/subdir/file.xml) and can only be changed manually and have the right privileges set (PHP can read and write to it).

I am using simplexml and the error I get is: I/O warning : failed to load external entity

So something strange is going on in the server, I just can't figure out what..

Edit: Adding some code

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

Solved: error in code, not server

  • 写回答

1条回答 默认 最新

  • drema2014 2012-05-06 15:40
    关注

    There are no obvious explanation why the file is occasionally unavailable. Could be a platform/OS issue, it's hard to tell.

    However, you could apply this band-aid on the problem, it may unblock you if waiting up to 3 seconds makes the file "re-appear":

    $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");
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度