Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php URL编码和文件获取内容_Php_File_Urlencode_File Get Contents - Fatal编程技术网

Php URL编码和文件获取内容

Php URL编码和文件获取内容,php,file,urlencode,file-get-contents,Php,File,Urlencode,File Get Contents,我们有一个类似http://site.s3.amazonaws.com/images/some image@name.jpg内部$string 我正在尝试做什么(是的,url周围有一个空格): 我得到的(@也被替换): file\u get\u contents(http%3A%2F%2Fsite.s3.amazonaws.com%2Fimages%2fome+image+@name.jpg)[function.file get contents]:打开流失败:没有这样的文件或目录 如果您复制/

我们有一个类似
http://site.s3.amazonaws.com/images/some image@name.jpg
内部
$string

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

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

file\u get\u contents(http%3A%2F%2Fsite.s3.amazonaws.com%2Fimages%2fome+image+@name.jpg)[function.file get contents]:打开流失败:没有这样的文件或目录

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


有什么不好以及如何修复?

您指定的URL无效
file\u get\u contents
需要一个有效的http URI(更准确地说,底层http包装器需要)。由于无效的URI不是有效的URI,
file\u get\u contents
失败

您可以通过将无效URI转换为有效URI来修复此问题。中提供了如何编写有效URI的信息。您需要注意正确表示所有特殊字符。e、 g.空格到加号,商业at标志必须进行URL编码。还需要删除开头和结尾处多余的空格


完成后,Web服务器将告诉您访问被禁止。然后,您可能需要通过HTTP文件包装器的HTTP上下文选项添加额外的请求头来解决这个问题。您可以在PHP手册中找到相关信息:

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

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

所以你把这个URL变成了一个无效的URL,然后你怀疑它不起作用?这是什么问题?请详细解释一下,我不能把我的头放在这上面。你能给我/我们提供你的完整代码吗?这是一个很好的答案这是一个很好的答案-:如何
RAWURLENCODE
$string = str_replace(' ', '%20', $string);