Php download script

Hi everybody. I’m new here and a beginner on PHP scripting. I’m trying to write a php code

in order to do this:

  1. start downloading a file from an HTTP server (an address like this:

http://free.com/get/0dw0bs/my_movie.flv). The file has about 500 Mb. The file is not on my

website host, it’s on a different host.
2. the file to be downloaded on my webserver on my folder named “videos” with a name like

this: my_movie_id.flv where id is a number that is incremented everytime the page is

accesed (this will be stored in mySQL database).
3. during download I want to be able to use my_movie_id.flv for streaming and if the page

is changed or the browser is closed I want this file to be deleted from my webserver.

I’ve tried and studied php codes for 2 weeks now and I haven’t been able to make it work.

I’ve write some php script but none of them downloaded all of the movie (only 10kb or

something like that).
If anyone can help or give me some ideas how can this be done I’ll really appreciate it.

Thanks a lot for your time to read this.

Anyone please …

This is the code i’ve found but something is wrong:


<?php

// File to download
$remoteFile = 'http://mkr5e5.dl4free.com/zece.flv';//when downloading with firefox save as ... the link is: http://glb-b2.dl4free.com/get/mkr5e5/zece.flv

// Local file for saving
$localFile = "videos/test.flv";

// Time to cache in hours
$cacheTime = 24;

// Connection time out
$connTimeout = 10;

if(file_exists($localFile) && (time() - ($cacheTime * 3600) < filemtime($localFile))){
     readfile($localFile);
}else{
     $url = parse_url($remoteFile);
     $host = $url['host'];
     $path = isset($url['path']) ? $url['path'] : '/';

     if (isset($url['query'])) {
          $path .= '?' . $url['query'];
     }

     $port = isset($url['port']) ? $url['port'] : '80';

     $fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout );

     if(!$fp){
          // If connection failed, return the cached file
          if(file_exists($localFile)){
               readfile($localFile);
          }
     }else{
          // Header Info
          $header = "GET $path HTTP/1.0\\r\
";
          $header .= "Host: $host\\r\
";
          $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\\r\
";
          $header .= "Accept: */*\\r\
";
          $header .= "Accept-Language: en-us,en;q=0.5\\r\
";
          $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\\r\
";
          $header .= "Keep-Alive: 300\\r\
";
          $header .= "Connection: keep-alive\\r\
";
          $header .= "Referer: http://$host\\r\
\\r\
";

           $response = '';
          fputs($fp, $header);
          // Get the file content
          while($line = fread($fp, 4096)){
               $response .= $line;
          }
          fclose( $fp );

          // Remove Header Info
          $pos = strpos($response, "\\r\
\\r\
");
          $response = substr($response, $pos + 4);
          echo $response;

          // Save the file content
          if(!file_exists($localFile)){
               // Create the file, if it doesn't exist already
               fopen($localFile, 'w');
          }
          if(is_writable($localFile)) {
               if($fp = fopen($localFile, 'w')){
                    fwrite($fp, $response);
                    fclose($fp);
               }
          }
     }
}

?>

When I test the code it appears the page where the link for download and the file created (test.flv) is not the flv file but an txt file that contains the html of the webpage where the download link is.
What I’m doing wrong?
Thanks