0

I'm writing a script to download files from Megaupload onto my server. I'm using cURL on PHP, I have the login script which downloads the cookie file:

<?php
function login($username, $password){
$mega = curl_init();
curl_setopt($mega, CURLOPT_URL, "http://www.megaupload.com/?c=login");
curl_setopt($mega, CURLOPT_POST, true);
curl_setopt($mega, CURLOPT_POSTFIELDS, "login=1&redir=1&username=$username&password=$password");
curl_setopt($mega, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_setopt($mega, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_exec($mega);
curl_close($mega);
}
?>

and the downloading script:

 <?php
    include("megaupload_login.php");
    login("username", "ps");
    set_time_limit(0);    
    $url = "http://www.megaupload.com/?d=A428CAKH";
    $fp = fopen("winch.zip", "w");
    $dl = curl_init($url);
    curl_setopt($dl, CURLOPT_COOKIEFILE, "megaupload_cookie.txt");
    curl_setopt($dl, CURLOPT_FILE, $fp);
    curl_exec($dl);
    curl_close($dl);
    fclose($fp);
  ?>

The problem is, the file doesn't download. All I get is a file named winch.zip with a size of 0 bytes. I think the program is actually downloading the login page, as when run the script the browser just shows the megaupload login page but the address is localhost. Any ideas on why this might not be working?

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
jz999
  • 589
  • 1
  • 5
  • 13
  • try the below link http://stackoverflow.com/questions/6103774/serving-downloads-with-php-and-curl – Nishant Dec 29 '11 at 07:40
  • Most download hosts issue a redirect to a tokenized/time limited download url. You haven't told curl to follow redirects via `CURLOPT_FOLLOWLOCATION` – Marc B Dec 29 '11 at 07:41
  • @marcb Awesome, the followlocation setting made it work, thanks so much. – jz999 Jan 03 '12 at 02:01

1 Answers1

2

Try Following for your download part of code:


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);    
    curl_close($ch);
    return $result;
}
$filecontents = download_file("http://www.megaupload.com/?d=A428CAKH");

Hope it helps

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162