【问题标题】:PHP Image DownloaderPHP 图像下载器
【发布时间】:2012-10-11 18:33:12
【问题描述】:

大家好,我正在寻求帮助,从我的一个供应商那里下载 1000 多张图片,用于电子商务网站。他们为我提供了我在数组中设置的正确 URL,但我似乎无法找到任何 PHP 脚本来功能性地下载图像。

我有:

ArrayOf1000ULRS[];

Loop through Array
   -save_image(URL)

我在网上找到的示例函数:

function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
    unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}

【问题讨论】:

  • if ($rawdata === false) { die(curl_error($ch)); } 是您可以拥有的一件额外的小东西。 curl 并不总是成功。
  • 另外,究竟是什么“不工作”?文件是否已创建(但为空)?还要确保 apache 用户可以写信给$fullpath

标签: php image curl download


【解决方案1】:
foreach($fileNames as $url)
{
set_time_limit(0);
//Get the filename from the end of the URL.
$imgName = explode("/", $url);
//Used this to run multiple scripts.
//Basically don't download files you have.
if(!file_exists("./images/".$imgName[-int-]))
{
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("./images/".$imgName[-int-],'w');
fwrite($fp, $rawdata);
fclose($fp);
}
}

【讨论】:

    【解决方案2】:

    假设您在 *nix 机器上,您可以在其中抛出一个系统调用并使用 WGET。

    foreach($arrayOfImages as $url){
        $cmd = "wget $url";
        system($cmd);
    }
    

    真不安全,但如果这是一次性私人的事情,我会说去做。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多