Page 1 of 1

PHP DOWNLOAD SCRIPT

Posted: Sun Sep 05, 2010 6:04 am
by gica.gica56
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.

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 05, 2010 10:59 am
by gica.gica56
My code is this:

Code: Select all

<html>
<form method="post">
<input name="url" size="50" value="http://glb-b3.dl4free.com/get/0dw0bs/The_Others.flv" />
<input name="submit" type="submit" />
</form>
<?php





// maximum execution time in seconds
set_time_limit (24 * 60 * 60);

if (!isset($_POST['submit'])) die();

// folder to save downloaded files to. must end with slash
$destination_folder = 'videos/';

$url = $_POST['url'];
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");

  if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}

if ($file) {
  fclose($file);
}

if ($newf) {
  fclose($newf);
}

?>

</html>
When I run the code it return me the page source of the url set for download. Anyone can help?

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 05, 2010 1:44 pm
by Jonah Bron
Do you mean you want to download the flv file to the user's computer, or to your server?

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 12, 2010 8:52 am
by gica.gica56
Hi. What I am trying to do is something like this:
1. upload my files on a server that can host big files;
2. when someone wants to play the file on a videoplayer (in my case I'm trying to use Flowplayer), i want to start downloading the file from the external server that hosts it on the server I have hosted my website (different servers), and use xmoovStream in order to send the downloaded data to the player to protect my file source (i don't know other method to securely protect my source). I need to start to send data to the player during the downloading from the source not after the download is fully done.
3. in case of browser closed or change of the page with the player, I want to stop downloading and delete to downloaded part of the file from the server I've hosted my website.

If you know another method to securely protect my source file please let me know.

Thanks.

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 12, 2010 11:13 am
by Jonah Bron
If you copy the file to your server every time, you might as well just host it there. I think it would be more simple to just set the video source in your Flash video player to the remote server.

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 12, 2010 12:06 pm
by gica.gica56
I can't do that since the host for my website doesn't allow too much (1.5 GB). So I need to make temporary short files for this.

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 12, 2010 1:56 pm
by Jonah Bron
That's what I'm saying. Since you don't have much space, you don't want to do the copying thing.

Re: PHP DOWNLOAD SCRIPT

Posted: Sun Sep 26, 2010 12:36 pm
by gica.gica56
Jonah Bron wrote:That's what I'm saying. Since you don't have much space, you don't want to do the copying thing.
No, I don't want to save the entire file on my webserver, I just want a temporary file for streaming which will be deleted when the view is finished.

Re: PHP DOWNLOAD SCRIPT

Posted: Mon Sep 27, 2010 4:02 am
by tonchily
gica.gica56 wrote:
Jonah Bron wrote:That's what I'm saying. Since you don't have much space, you don't want to do the copying thing.
No, I don't want to save the entire file on my webserver, I just want a temporary file for streaming which will be deleted when the view is finished.
But why? You can set video source in your video player to http://free.com/get/0dw0bs/my_movie.flv and you don't have to download files to your server at all, and your visitors will still be able to watch the video same as if you have downloaded it.

Re: PHP DOWNLOAD SCRIPT

Posted: Mon Sep 27, 2010 4:58 am
by DigitalMind
If you want to hide the source site, use http proxy. Also it's possible to use red5 server for streaming. Using PHP is a wrong approach for your goal.

Re: PHP DOWNLOAD SCRIPT

Posted: Sat Oct 09, 2010 10:39 am
by gica.gica56
DigitalMind wrote:If you want to hide the source site, use http proxy. Also it's possible to use red5 server for streaming. Using PHP is a wrong approach for your goal.
I'm using an external webserver for testing (000webhost) so I can't install everything on it. I've searched about red5 server and it seems that it needs instal, so ...

Re: PHP DOWNLOAD SCRIPT

Posted: Sat Oct 09, 2010 10:46 am
by gica.gica56
This is my code:

Code: Select all

<?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);
               }
          }
     }
}

?>
And 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