自动下载zip文件脚本

时间:2010-10-13 06:15:40

标签: php

我想编写一个脚本,它将从给定的URL下载zip文件并将其保存在我的硬盘中。 URL看起来像这样。 http://localhost/downloads/1http://localhost/downloads/1。我正在尝试这样

<?php
for($i=1;$i<=100;$i++){
    $zipfile=file_get_contents('http://localhost/downloads'.$i);
    echo $zipfile;}

但它不起作用。 我想在localhost上尝试这个脚本。 id将为我下载歌曲,图片。

1 个答案:

答案 0 :(得分:3)

这是因为您的网址类似于http://localhost1http://localhost2 ....请注意缺少的/

另外,要保存下载的内容,请使用file_put_contents功能而不是echo。这需要在循环内部完成:

for($i=1;$i<=100;$i++) {
   $zipfile=file_get_contents('http://localhost/downloads/'.$i);
   file_put_contents('some/other/dir/'.$i.'zip',$zipfile);
}

由于您要从localhost复制到localhost,因此您可以更好地使用copy功能。