dpca4790 2012-05-08 11:24
浏览 209
已采纳

Simplexml_load_string可以直接链接到XML文件,但不能与file_get_contents('php:// input')一起使用

Searched a lot for this on StackOverflow and Google but can't seem to find an answer to this specific situation

I'm working with a client that sends me an XML file via PHP POST and this is the code I use:

$xml = file_get_contents('php://input');
$xmlStripped = stripslashes($xml);
$xmlContent = simplexml_load_string($xmlStripped);
print_r($xmlContent);

For testing I also uploaded the XML file directly to my server and did it like this:

$xml = file_get_contents('http://www.site.com/linktoxml.xml');
$xmlStripped = stripslashes($xml);
$xmlContent = simplexml_load_string($xmlStripped);
print_r($xmlContent);

And then it works fine, it prints out the XML as an object.

The form I use to upload is like this:

<form action="http://app.site.com/upload" method="POST">
<input type="file" name="file" id="file ">
<input type="submit" value="upload">
</form>
  • 写回答

2条回答 默认 最新

  • duanran3115 2012-05-08 11:28
    关注

    file_get_contents('php://input'); would normally give you the raw RFC 1867 data, not the file itself. The file data is enveloped inside this request data. In your case, it gives an empty string because PHP discards such data after processing it in order to save memory.

    PHP, however, automatically processes and decodes the RFC 1867 data. What you have to do is (besides validating that $_FILES['file']['tmp_name'] exists and is a string):

    $xml = file_get_contents($_FILES['file']['tmp_name']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题