【发布时间】:2016-03-09 05:16:20
【问题描述】:
在你的帮助下,许多 stackoverflow 帖子,解决方案在这篇帖子的底部作为更新。
我正在尝试使用此代码在 php 文件中自动保存一些图像:
for ($num1=100;$num1<999;$num1++)
{
for ($num2=100;$num2<999;$num2++)
{
$postURL = "http://link_00000'.$num1.'_'.$num2.'.jpg";
$ch = curl_init('http://link_00000'.$num1.'_'.$num2.'.jpg');
$fp = fopen($postURL, '/path/Apolo/img/'.$num1.'_'.$num2.'.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
第一个问题:
$num1 和 $num2 应该从 000 到 999 而不是从 0 到 999 开始。放置更多 $num 变量($num3,$num4 ...)将是一个解决方案,但我认为有一个更好的数字。
第二个问题:
图像未保存。我也试过这个,但没有用:
copy($postURL, '/path/img/'.$num1.'_'.$num2.'.jpg');
第三个问题:
如何防止像 d-dos 攻击这样的行为?如果我没有延迟加载链接,网站可能会关闭。
更新:
for ($num1=000;$num1<999;$num1++)
{
for ($num2=000;$num2<999;$num2++)
{
$url = 'http://link.com/00000'.sprintf("%03d", $num1).'_'.sprintf("%03d", $num2).'.jpg';
echo ''.$num1.'_'.$num2.'';
echo "\n";
if (@getimagesize($url)) \\ checks if url-image exists
{
echo $url;
$ch = curl_init($url);
$fp = fopen('/path/Apolo/00000'.sprintf("%03d", $num1).'_'.sprintf("%03d", $num2).'.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
sleep(1); //1 second delay to avoid d-dos
}
}
}
【问题讨论】:
-
看看如何处理for循环的例子:php.net/manual/en/control-structures.for.php
-
您错误地连接了
$postURL值中的变量。尝试使用单引号作为字符串分隔符而不是双引号。 -
为什么您最终使用了包含大量 for 循环和数字的解决方案,而不是仅使用自动添加前导零的
sprintf("%03d", $num1)。
标签: php digit ddos save-image