PHP DOWNLOAD SCRIPT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

PHP DOWNLOAD SCRIPT

Post 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.
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP DOWNLOAD SCRIPT

Post by Jonah Bron »

Do you mean you want to download the flv file to the user's computer, or to your server?
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP DOWNLOAD SCRIPT

Post 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.
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP DOWNLOAD SCRIPT

Post 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.
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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.
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: PHP DOWNLOAD SCRIPT

Post 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.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: PHP DOWNLOAD SCRIPT

Post 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.
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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 ...
gica.gica56
Forum Newbie
Posts: 7
Joined: Sun Sep 05, 2010 5:42 am

Re: PHP DOWNLOAD SCRIPT

Post 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
Post Reply