簡體   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