2010-10-13 91 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功能。