使用PHP从服务器下载文件

I am having a web server in which there are 1000 files in a folder with the following name:

1.txt
2.txt
3.txt
.
.
.
1000.txt

Note: files might be in some other format also. Like JPEG, PNG etc. I want to download them all using PHP (I am having access to that folder).

I am trying to use the following algorithm:

for (i=1 to 1000)
{
     $link = "http://xyz.com/pqr/".$i.".txt";
     fopen($link);
     /*download -- how to?*/
}

Is this method correct? How to copy / download it into my local machine?

Use this: http://php.net/manual/en/function.file-get-contents.php

And then save the value in a file with the same name locally: http://www.php.net/manual/en/function.fwrite.php

You can use cURL:

function get_data($link)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}