2014-10-04 104 views
-2

給定一個指向文件的URL,如何打開它並讀取PHP中的內容?打開一個文件的URL並在PHP中讀取?

$file = fopen("http://www.alinktomyfile.com/kasfafa", "r"); 

此行有錯誤。該docs說使用stream_get_contents()的URL,但是我得到一個錯誤:

$file_contents = stream_get_contents($file); 
fclose($file); 

錯誤:

警告:stream_get_contents()預計參數1是資源,空鑑於/用戶/唐納德在線/Desktop/php_boilerplate.php 24

+0

如果你要downvote,請留言說明原因。謝謝 – 2014-10-04 05:39:05

+2

我相信'filesize()'不適用於HTTP。你應該使用'file_get_contents()'來代替。 – 2014-10-04 05:39:38

回答

0

嘗試file_get_contents()

$file = file_get_contents("http://www.alinktomyfile.com/kasfafa"); 
+0

謝謝Rakesh,你知道爲什麼file_get_contents比stream_get_contents更可取嗎? – 2014-10-04 05:44:01

+0

與file_get_contents()相同,不同之處在於stream_get_contents()對已打開的流資源進行操作,並返回字符串中的剩餘內容,直到最大長度字節並從指定的偏移量開始。 – 2014-10-04 05:45:01

相關問題