php - urlencode 和 file_get_contents

标签 php file urlencode file-get-contents

我们在 $string 中有一个类似 http://site.s3.amazonaws.com/images/some image @name.jpg 的 url>

我正在尝试做什么(是的,url 周围有一个空格):

$string = urlencode(trim($string));
$string_data = file_get_contents($string);

我得到的(@也被替换):

file_get_contents(http%3A%2F%2Fsite.s3.amazonaws.com%2Fimages%2Fsome+image+@name.jpg)[function.file-get-contents]: 无法打开流:没有这样的文件或目录

如果您将 http://site.s3.amazonaws.com/images/some image @name.jpg 复制/粘贴到浏览器地址栏,图像将打开。

有什么问题以及如何解决?

最佳答案

对整个 URL 使用函数 urlencode() 将生成无效的 URL。保留 URL 也是不正确的,因为与浏览器相比,file_get_contents() 函数不执行 URL normalization。 .在您的示例中,您需要用 %20 替换空格:

$string = str_replace(' ', '%20', $string);

关于php - urlencode 和 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071257/

相关文章:

java - 如何避免与 file.getParentFile() 进行 null 比较?

python - Http POST 重音编码

python - 在 Python 2.7 中打开 URL 时返回乱码

php - 多维数组使用 foreach 返回错误结果

php - 在mysql中保存数据但不能

javascript - 如何在没有 iFrame 的情况下在 html 上显示来自另一台服务器的 PHP 页面?

php - 为什么我不能在 PHP 的 DateTime 类中访问 DateTime->date?

android - 有没有办法从 Android 的 Assets 文件夹中将文件作为 File 对象打开?

file - 如何将文本文件保存到特定文件夹

python - 有没有办法禁用 lxml 中 anchor 属性的 urlencoding