I need php script for resumable file download from url to server. It should be able to start download, then when it snaps (30 sec- 5 min) resume, and so on until it completes whole file.

There is something similar in perl <http://curl.haxx.se/programs/download.txt> , but I want to do it in php, I don't know perl.

I think using `CURLOPT_RANGE` to download chunks, and `fopen($fileName, "a")` to append it to file on server.

Here is my try:

```
<?php

function run()
{
    while(1)
    {
         get_chunk($_SESSION['url'], $_SESSION['filename']);
         sleep(5);
         flush();
    }    
}

function get_chunk( $url, $fileName)
{

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if (file_exists($fileName)) {
        $from = filesize($fileName);
        curl_setopt($ch, CURLOPT_RANGE, $from . "-");//maybe "-".$from+1000 for 1MB chunks
    }

    $fp = fopen($fileName, "a");
    if (!$fp) {
        exit;
    }
    curl_setopt($ch, CURLOPT_FILE, $fp);
    $result = curl_exec($ch);
    curl_close($ch);

    fclose($fp);

}

?>

```