【问题标题】:PHP - Copy image to my server direct from URL [duplicate]PHP - 直接从 URL 将图像复制到我的服务器 [重复]
【发布时间】:2011-09-12 12:16:16
【问题描述】:

可能重复:
save image from php url using php

我想有 PHP 代码供关注。

假设我有一个图片网址,例如http://www.google.co.in/intl/en_com/images/srpr/logo1w.png

如果我运行一个脚本,此图像将被复制并放在我的服务器上的具有 777 权限的文件夹中。

有可能吗?如果是的话,你能指点一下吗?

谢谢,

伊恩

【问题讨论】:

标签: php


【解决方案1】:

两种方式,如果您使用的是 PHP5(或更高版本)

copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.png');

如果没有,请使用file_get_contents

//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.png", "w");
fwrite($fp, $content);
fclose($fp);

来自this SO post

【讨论】:

  • 非常感谢! copy() 成功了
  • 用位图图片代替url可以吗?
  • 没有一个对我有用..我正在尝试在codeignitier中实现它..但什么也没发生..:(
  • 感谢 Dotty,您的代码对我帮助很大。
  • 在此示例中,源文件和目标文件的文件扩展名不同。文件会从png转换为jpg吗?
【解决方案2】:

来自Copy images from url to server, delete all images after

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('tmp/'.$imagename,$image);       

【讨论】:

  • 谢谢..这个只能正常工作..谢谢迈克尔
  • 谢谢。它对我很有用!
【解决方案3】:
$url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents=file_get_contents($url);
$save_path="/path/to/the/dir/and/image.jpg";
file_put_contents($save_path,$contents);

您必须将allow_url_fopen 设置为on

【讨论】:

  • 我想你可能想澄清一下 $url 的第二次使用与第一次不同。
  • 无法连接,因为目标机器主动拒绝。 M 收到此错误。
【解决方案4】:

这个SO thread 将解决您的问题。简而言之:

$url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png';
$img = '/my/folder/my_image.gif';
file_put_contents($img, file_get_contents($url));

【讨论】:

    【解决方案5】:
    $url = "http://www.example/images/image.gif";
    $save_name = "image.gif";
    $save_directory = "/var/www/example/downloads/";
    
    if(is_writable($save_directory)) {
        file_put_contents($save_directory . $save_name, file_get_contents($url));
    } else {
        exit("Failed to write to directory "{$save_directory}");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 1970-01-01
      • 2016-08-01
      • 2010-11-20
      • 2013-06-22
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多