简体   繁体   English

PHP中的海量图像下载脚本

[英]Mass image download script in PHP

I need a .php script that will download a lot of images from another site. 我需要一个.php脚本,它将从另一个站点下载大量图像。 The images are thumbs - each has about 20KB size. 图像是拇指 - 每个大小约20KB。 I have worked on my own script, but sadly it just lags my server and nearly kills it forcing me to restart it. 我已经开发了自己的脚本,但遗憾的是它只是滞后于我的服务器而且差点杀死它迫使我重新启动它。

There are about 100 pictures or more per execution, .jpg files, ~20KB / file. 每次执行大约有100张图片,.jpg文件,~20KB /文件。

My script: 我的剧本:

$count = 0;
foreach ($files as $file) {
$count++;
$url = $file;
$dl_place = '/home/lulz/'.$count.'.jpg';

$ch = curl_init($dl);
$fp = fopen($path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

As you see I am using curl, but I am willing to use anything if it just works better than it is now. 如你所见,我正在使用curl,但如果它比现在更好用,我愿意使用任何东西。

Chances are, what is slowing down is the time it takes to set up all of these requests. 机会是,减慢的是设置所有这些请求所需的时间。 You should consider Parallel cURL to download multiple at a time. 您应该考虑使用Parallel cURL一次下载多个。 Source code: https://github.com/petewarden/ParallelCurl/blob/master/parallelcurl.php 源代码: https//github.com/petewarden/ParallelCurl/blob/master/parallelcurl.php

$pc->startRequest('http://www.whatever.com/someimage.jpg', 'your_callback_function');

I have also found that with library, you can use anonymous functions instead of the name of a function in your callback. 我还发现,使用库,您可以在回调中使用匿名函数而不是函数名。 I use this to call another function with an ID number, for example. 例如,我使用它来调用具有ID号的另一个函数。

$requestid=37;
$pc->startRequest(
    $url, 
    function($content, $url, $ch, $search) use $requestid {
        yourRealCallback($content, $url, $ch, $search, $requestid);
    }
);

This utilizes an anonymous function with closure so that if you are searching a DB of URLs, you can get the resulting ID (that you specify in a for loop or something... hard-coded to '37' here for demonstration). 这利用了一个带闭包的匿名函数,这样如果你正在搜索URL的数据库,你可以得到结果ID(你在for循环中指定的东西......在这里硬编码为'37'用于演示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM