SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone please ...

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the code i've found but something is wrong:

    Code:
    <?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\n";
              $header .= "Host: $host\r\n";
              $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\n";
              $header .= "Accept: */*\r\n";
              $header .= "Accept-Language: en-us,en;q=0.5\r\n";
              $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
              $header .= "Keep-Alive: 300\r\n";
              $header .= "Connection: keep-alive\r\n";
              $header .= "Referer: http://$host\r\n\r\n";
    
               $response = '';
              fputs($fp, $header);
              // Get the file content
              while($line = fread($fp, 4096)){
                   $response .= $line;
              }
              fclose( $fp );
    
              // Remove Header Info
              $pos = strpos($response, "\r\n\r\n");
              $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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •