2013-06-11 41 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()来生成路径。

然后将路径传递给您的代码。

由于该文件现在是本地的,它应该上传得很好。