$guestbook = new DomDocument(); //创建一个新的 DOM对象
$guestbook->load('guestbook.xml'); //读取 XML数据
$threads = $guestbook->documentElement; //获得 XML结构的根
//创建一个新 thread节点
$thread = $guestbook->createElement('thread');
$threads->appendChild($thread);
//在新的 thread节点上创建 title标签
$title = $guestbook->createElement('title');
$title->appendChild($guestbook->createTextNode($_POST['title']));
$thread->appendChild($title);
//在新的 thread节点上创建 author标签
$author = $guestbook->createElement('author');
$author->appendChild($guestbook->createTextNode($_POST['author']));
$thread->appendChild($author);
//在新的 thread节点上创建 content标签
$content = $guestbook->createElement('content');
$content->appendChild($guestbook->createTextNode($_POST['content']));
$thread->appendChild($content);
//=========================================================
$threads->appendChild($guestbook->createTextNode("\n"));//这样来添加换行
//=========================================================
//将 XML数据写入文件
$fp = fopen("guestbook.xml", "w");
if(fwrite($fp, $guestbook->saveXML()))
{
//fwrite($fp,"\r\n");
echo "成功";
echo " <script language='Javascript'>
      parent.location.reload();
        </script>";
}
else
echo "留言提交失败";
fclose($fp); 做了个留言板,php操作XML本地测试成功,上传到服务器就出错,显示留言内容那部分出错Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "guestbook.xml" in E:\www\14598\3.php on line 4 Notice: Trying to get property of non-object in E:\www\14598\3.php on line 5 Warning: Invalid argument supplied for foreach() in E:\www\14598\3.php on line 5 请问是什么问题呢?该怎么修改? 
有guestbook.xml这个文件 
3.php这行就是$guestbook=simplexml_load_file('guestbook.xml');和下面行 
foreach($guestbook->thread as $th)

解决方案 »

  1.   

    是什么服务器?如果是 linux 服务器的话chmod 755 guestbook.xml
      

  2.   

    ////////////////////////////////////////////////////////* 取得当前FILE所在的根目录 */define ('GUESTBOOK', "dirname(__FILE__)/gusetbook.xml");///////////////////////////////////////////////////////$guestbook = new DomDocument(); //创建一个新的 DOM对象
    $guestbook->load(GUESTBOOK); //读取 XML数据
    $threads = $guestbook->documentElement; //获得 XML结构的根
    //创建一个新 thread节点
    $thread = $guestbook->createElement('thread');
    $threads->appendChild($thread);
    //在新的 thread节点上创建 title标签
    $title = $guestbook->createElement('title');
    $title->appendChild($guestbook->createTextNode($_POST['title']));
    $thread->appendChild($title);
    //在新的 thread节点上创建 author标签
    $author = $guestbook->createElement('author');
    $author->appendChild($guestbook->createTextNode($_POST['author']));
    $thread->appendChild($author);
    //在新的 thread节点上创建 content标签
    $content = $guestbook->createElement('content');
    $content->appendChild($guestbook->createTextNode($_POST['content']));
    $thread->appendChild($content);
    //=========================================================
    $threads->appendChild($guestbook->createTextNode("\n"));//这样来添加换行
    //=========================================================
    //将 XML数据写入文件
    $fp = fopen("guestbook.xml", "w");
    if(fwrite($fp, $guestbook->saveXML()))
    {
    //fwrite($fp,"\r\n");
    echo "成功";
    echo " <script language='Javascript'>
          parent.location.reload();
            </script>";
    }
    else
    echo "留言提交失败";
    fclose($fp); 
      

  3.   

    if(!isset($_SERVER['DOCUMENT_ROOT'])) 
    {    
      if(isset($_SERVER['SCRIPT_FILENAME'])) 
      { 
      $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF']))); 
      } 

    if(!isset($_SERVER['DOCUMENT_ROOT'])) 

      if(isset($_SERVER['PATH_TRANSLATED'])) 
      { 
      $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0-strlen($_SERVER['PHP_SELF']))); 
      } 

       function request_uri() {
        if (isset($_SERVER['REQUEST_URI']))     {
              $uri = $_SERVER['REQUEST_URI'];
      }
    else{
       if (isset($_SERVER['argv'])){
         $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
    }
    else {
     $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
    }
    }
     return $uri;
       }
    ------------------------------------------------------------------
    路径= $_SERVER['DOCUMENT_ROOT'].dirname(request_uri())
    url= "http://".$_SERVER ['HTTP_HOST'].dirname(request_uri())我不知道是否对你有帮助,我自己的上传文件页是这么写的,本地和远程的虚拟主机都能通过