代码之家  ›  专栏  ›  技术社区  ›  Brock Woolf

为get/post请求输出php文件的内容

  •  1
  • Brock Woolf  · 技术社区  · 15 年前

    我有一个脚本,它在用户访问链接时生成一个XML文件,如下所示:

    域.com/dirlist.php

    我想在POST或GET请求结束时将XML的内容打印给用户(还没有实现这一点)。目前,我只是通过网络浏览器查看链接。

    这是我用来打印文件内容的代码。

    # Open XML file and print contents
    $filename = "test.xml";
    $handle = fopen ($filename, "r"); 
    $contents = fread ($handle, filesize ($filename)); 
    fclose ($handle); 
    print $contents;
    

    问题是它似乎不起作用。我想我可能缺少这样的标题: ("Content-Type: text/xml"); 或者我的代码有误。

    我需要做什么才能让XML文件打印到浏览器?

    2 回复  |  直到 15 年前
        1
  •  0
  •   codaddict    15 年前

    你只需做:

    header('Content-type: text/xml'); // set the correct MIME type before you print.
    readfile('test.xml'); // output the complete file.
    
        2
  •  0
  •   Pramendra Gupta    15 年前
    //Check there is any extra space before <? and after ?>
    
    //or 
    
    header('Content-type: text/xml');
    echo file_get_contents($path.$xmlfile);