打开流失败:HTTP 请求失败! HTTP/1.1 400 错误请求
我正在从另一个网站访问图像。我收到此错误:
复制“某些(不是全部)”图像时出现“无法打开流:HTTP 请求失败!HTTP/1.1 400 错误请求”错误。这是我的代码。
$img=$_GET['img']; //another website url
$file=$img;
function getFileextension($file) {
return end(explode(".", $file));
}
$fileext=getFileextension($file);
if($fileext=='jpg' || $fileext=='gif' || $fileext=='jpeg' || $fileext=='png' || $fileext=='x-png' || $fileext=='pjpeg'){
if($img!=''){
$rand_variable1=rand(10000,100000);
$node_online_name1=$rand_variable1."image.".$fileext;
$s=copy($img,"images/".$node_online_name1);
}
I am accessing images from another website. I am getting this Error:
"failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request " error when copying 'some(not all)' images. here is my code.
$img=$_GET['img']; //another website url
$file=$img;
function getFileextension($file) {
return end(explode(".", $file));
}
$fileext=getFileextension($file);
if($fileext=='jpg' || $fileext=='gif' || $fileext=='jpeg' || $fileext=='png' || $fileext=='x-png' || $fileext=='pjpeg'){
if($img!=''){
$rand_variable1=rand(10000,100000);
$node_online_name1=$rand_variable1."image.".$fileext;
$s=copy($img,"images/".$node_online_name1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为 preg_replace 更有意义,因为它可以与最新版本的 PHP 一起使用,因为 ereg_replace 不适合我,已被弃用
I think preg_replace make more better sense as it will work with latest versions of the PHP as ereg_replace didn't worked for me being deprecated
解决了这个问题。
我遇到了同样的问题,但感谢 Cello_Guy 的帖子
I had the same problem, but it was solve by
Thanks Cello_Guy for the post.
我能想到的唯一问题是网址中存在空格,很可能是文件名中存在空格。 url 中的所有空格都需要转换为其正确的编码,即 %20。
如果您的文件名如下:
“http://www.somewhere.com/images/img 1.jpg"
你会得到上面的错误,但是这样:
"http:// /www.somewhere.com/images/img%201.jpg”
你应该遇到问题。
只需使用
str_replace()
将空格 (" ") 替换为其正确的编码 ("%20")它看起来像这样:
有关
str_replace()
的更多信息> 查看 PHP 手册 。The only issue I can think of is spaces being in the url, most likely in the file name. All spaces in a url need to be converted to their proper encoding, which is %20.
If you have a file name like this:
"http://www.somewhere.com/images/img 1.jpg"
You would get the above error, but with this:
"http://www.somewhere.com/images/img%201.jpg"
You should have to problems.
Just use the
str_replace()
to replace the spaces (" ") for their proper encoding ("%20")It looks like this:
For more information on the
str_replace()
check out The PHP Manual.使用函数
rawurlencode()
根据 » RFC 3986 对给定字符串进行编码。
http://php.net/manual/ru/function.rawurlencode.php
Use the function
rawurlencode()
Encodes the given string according to » RFC 3986.
http://php.net/manual/ru/function.rawurlencode.php
即使 url 中的尾随空白也会导致 php file($url) 失败。在最新版本的 php 或 apache 中,即使 url 中的尾随空白也会导致错误。因此,该 url 似乎可以在浏览器中使用,因为浏览器足够了解 %20 尾随空白或忽略它。无论如何,那是我的错误。
较旧的 LAMP 允许这样做。 (即相同的代码运行正常)。轻松修复。
Even a trailing blank in the url can cause php file($url) to fail. In recent versions of php or apache even a trailing blank in the url will cause the error. So the url appears to work in a browser because the browser knows enough to %20 the trailing blank or ignore it. That was my error anyway.
Older LAMP allowed it. (ie. same code ran ok). Easy fix.
看来URL中有一些空格或其他特殊字符,需要对其进行编码,使用urlencode()函数
It seems that the URL has some spaces or other special characters, you need to encode it, use urlencode() function