2010-10-13 97 views
0

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

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

但它不起作用。 我想在本地主机上尝试这个脚本。 id会下载歌曲,照片给我。

+1

“'http:// localhost/downloads/1'到'http:// localhost/downloads/1'”...?咦? – deceze 2010-10-13 06:22:24

+0

纠正了我的错误。 – mysterious 2010-10-13 06:34:43

回答

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功能。