繁体   English   中英

PHP-从远程服务器复制图像时的大问题

[英]PHP - Big Problems When Copying Images from Remote Server

我想将图片从远程复制到我的服务器,但有时会复制错误的图像。我几乎尝试了所有解决方案,但下面以最简单的示例为例。

这个问题很奇怪。 如果我从curl中解析一个值为http://www.domain.com/image.jpg的变量,然后从中下载图像,则会得到错误的图像。

1)这不起作用($ image的值为http://www.domain.com/image.jpg

//url of a picture
$image = $result->xpath('image-url');
$image = (string)$image[0];


copy($image, '/patch/image.jpg');

2)的作品-当我直接定义图片网址。

//url of a picture pulled by curl
$image = $result->xpath('image-url');
$image = (string)$image[0];


if($image == 'http://www.domain.com/image.jpg') {

 $image = 'http://www.domain.com/image.jpg';
}


copy($image, '/patch/image.jpg');

在这两种情况下,$ image的值完全相同,但是第一个有时下载错误的图像,而第二个总是下载正确的图像。

你能帮忙吗?

 I have tried few variation:
 1) $img = file_get_contents('http://placehold.it/150x150');  - WORKS
 2) $img = file_get_contents('http://www.domain.com/image.jpg');  -WORKS
 3) $img = file_get_contents($image);   where  
 var_dump($image) = string(66) "http://www.domain.com/image.jpg"
 echo $image =   http://www.domain.com/image.jpg Doesn't work.

这根本没有逻辑:

if($image == 'http://www.domain.com/image.jpg') {

 $image = 'http://www.domain.com/image.jpg';
}

尝试这个:

$img = file_get_contents('http://placehold.it/150x150');
$fh = fopen('path/to/image/new', 'w+');
if (is_resource($fh)) {
    fwrite($fh, $img);
    fclose($fh);
}

在这种情况下,我不使用copy()
最好使用file_get_contents()curl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM