【问题标题】:Open a file URL and read it in PHP?打开一个文件 URL 并用 PHP 读取它?
【发布时间】:2014-10-04 05:33:33
【问题描述】:

给定一个指向文件的 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 是资源,在第 24 行的 /Users/donald/Desktop/php_boilerplate.php 中给出 null

【问题讨论】:

  • 如果您要投反对票,请留言说明原因。谢谢
  • 我相信 filesize() 不适用于 HTTP。你应该改用file_get_contents()

标签: php file fopen


【解决方案1】:

试试file_get_contents()

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

【讨论】:

  • 谢谢 Rakesh,你知道为什么 file_get_contents 比 stream_get_contents 更可取吗?
  • 与 file_get_contents() 相同,不同之处在于 stream_get_contents() 对已打开的流资源进行操作并以字符串形式返回剩余内容,最长为 maxlength 个字节并从指定的偏移量开始。
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多