1

I am writing a script that serves users downloads using my Megaupload premium account. This is the method that I'm using to download the Megaupload files:

public function download_file($link)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);
    curl_close($ch);
}

And this is how it's being called in index.php:

readfile($megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM"));

What I basically want this to do is serve the download to the user and stream the download through my server. I do not want the file to be saved to my server and then offered to the user.

But obviously, the solution above won't work. I get this error:

Warning: readfile() [function.readfile]: Filename cannot be empty in C:\xampp\htdocs\index.php on line 8

I need to use cURL for this because I need to request the download using a cookie file (/cookies/megaupload.txt).

Can anyone think of a working way to do this? Cheers.

Josh
  • 1,361
  • 5
  • 22
  • 33

3 Answers3

2

Using the method you're trying to use won't work (or won't work like you think).

Curl will block until the file is completely downloaded.

You have to create your own protocol, and handle the write event within the class.

Here's a link to a previous discussion where someone has done a very good job of explaining it. Basically you need to process the file in manageable chunks and hand them off to the client's browser: Manipulate a string that is 30 million characters long

You could also try using sockets, but you're going to have to work on your cookie code, because curl handles a lot of that behind the scenes for you.

Community
  • 1
  • 1
gnxtech3
  • 782
  • 3
  • 7
1

Yes, exactly as it says, readfile expects a filename, and you're giving it a string of the curl result. Just use echo after setting the proper headers:

echo $megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM");

EDIT: Also you need to do some basic checks that you got something back:

public function download_file($link)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    // close before you return
    curl_close($ch);
    return $result;
}

Then:

$filecontents = $megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM");
if($filecontents) {
  echo $filecontents;
}
else {
  die("Yikes, nothing here!");
}
onteria_
  • 68,181
  • 7
  • 71
  • 64
-1

Algorithm for that

  1. retrieve the direct URL path of the desired file
  2. tell your script to wait for 45 seconds (non member) or 25 seconds (member)
    • if in case the user is a member, user must login first
  3. download the file
macki
  • 904
  • 7
  • 12