2013-06-11 156 views
0

我試圖將照片張貼到Facebook和只要圖像是在同一個文件夾中的PHP腳本它的作品上傳圖片:PHP - 從外部服務器

$file= "myimage.png"; 
    $args = array(
     'message' => 'Photo from application', 
     ); 
     $args[basename($file)] = '@' . realpath($file); 
    $ch = curl_init(); 

我需要做什麼來改變,使之成爲外部圖像工作,比如:

$file= "http://www.example.com/myimage.png"; 
+0

http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php – transilvlad

+0

http://stackoverflow.com/questions/909374/copy-image-from-remote-server -over-http – transilvlad

+0

在那裏,我找到了兩個答案給你一個好的Google搜索。 – transilvlad

回答

3

您必須將圖像先下載到您的服務器,然後使用該路徑。下面是將圖像下載到臨時文件的示例:

$temp_name = tempnam(sys_get_temp_dir(), "external"); 
copy($file, $temp_name); 
// ... 
$args[basename($file)] = '@' . realpath($temp_name); 
1

確保文件無損下載我更喜歡這種方式。

$path = '/where/to/save/file'; 
$url = 'http://path.to/file'; 

$remote = fopen($url, "rb"); 
if($remote) { 
    $local = fopen($path, "wb"); 
    if($local) { 
     while(!feof($remote)) { 
      fwrite($local, fread($remote, 1024 * 8), 1024 * 8); 
     } 
    } 
} 
if ($remote) fclose($remote); 
if ($local) fclose($local); 

我推薦使用uniqid()來生成路徑。

然後將路徑傳遞給您的代碼。

由於該文件現在是本地的,它應該上傳得很好。