mysterious
mysterious

Reputation: 1478

auto download zip files script

I want to write a script that will download zip files from a given url and save it somewhere in my hard disk. URL looks like this. http://localhost/downloads/1 to http://localhost/downloads/1. I am trying it like this

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

but it won't work. I wanted to try this script on localhost. id will be downloading songs, pics for me.

Upvotes: 0

Views: 946

Answers (1)

codaddict
codaddict

Reputation: 455470

That's because your URL are like http://localhost1, http://localhost2....Notice the missing /.

Also to save the downloaded content you use the function file_put_contents not echo. And this needs to be done inside the loop as:

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

Since you're copying from localhost to localhost you can better use the copy function.

Upvotes: 3

Related Questions