2012-05-08 16 views
0

在StackOverflow和Google上搜索了很多,但似乎無法找到答案具體情況Simplexml_load_string可以直接連接到XML文件,但不會與file_get_contents('php:// input')一起工作

我與發送我通過PHP POST一個XML文件中的客戶端的工作,這是我使用的代碼:

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

爲了測試我也直接上傳XML文件到我的服務器和它是這樣的:

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

然後它工作正常,它將XML作爲對象打印出來。

我用上傳的形式是這樣的:

<form action="http://app.site.com/upload" method="POST"> 
<input type="file" name="file" id="file "> 
<input type="submit" value="upload"> 
</form> 
+0

你在接下來的階段中得到什麼?第一種情況下$ xml和$ xmlStripped變量的內容是什麼? – petr

+0

$ xml打印文件= xmlfilename.xml和$ xmlStripped爲空 – drilboor

回答

0

file_get_contents('php://input');通常會給你原始的RFC 1867數據,而不是文件本身。文件數據封裝在請求數據中。在你的情況下,它會給出一個空字符串,因爲PHP在處理它之後會丟棄這些數據以節省內存。

但是,PHP會自動處理和解碼RFC 1867數據。你必須做的是(除了驗證$_FILES['file']['tmp_name']存在並且是一個字符串):

$xml = file_get_contents($_FILES['file']['tmp_name']); 
+0

謝謝!這固定它! – drilboor

0

嘗試使用實際的文件從

$_FILES['userfile']['tmp_name'] 

代替

"php://input" 

如果您堅持使用php://輸入,試試這個hack

+0

謝謝!這固定它! – drilboor

相關問題