views:

1525

answers:

2

Hello all,

I can download remote files using PHP but how do you download from a link that pushes headers out? I mean, you can click on some links and it will force a download and present you with dialog box to save the file. How can I download and save this sort of thing using PHP?

Any examples or links to tutorials would be great since I couldn't find anything useful on this topic.

Thank you for any help

Updated and [SOLVED]

<?php

set_time_limit(300);

// File to download
$remoteFile = $_GET['url'];

$file = fopen($remoteFile, "r");


if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
}
$line = '';

while (!feof ($file)) {
    $line .= fgets ($file, 4096);
}

//readfile($line);
file_put_contents('here2.mp4',  $line);

fclose($file);

?>
+1  A: 

You can do it the same way as you download your remote files. Those “force download” header values just tell user agents that want to display the data inline to download them instead. But it makes no difference for your script as it cannot display the data.

Gumbo
+2  A: 

Just tried to reproduce situation. Gubmo is right, this download method works for me with Content-Type: application/octet-stream and Content-type: application/force-download headers.

As explained here, HTTP 410 means that URL requested by the client is no longer available from that system. This is not a 'never heard of it' response, but a 'does not live here any more' response. Maybe they have some kind of antileach system.

This should be investigated. If they need cookies -- stream-context-create can help. Or maybe they check referer. But I am almost sure that problem is not in headers.

Hope this helps.

UPD Sample code you've asked about.

// file to download -- application/octet-stream
$remoteFile = 'http://dev/test/remote/send.php';
// file to download -- application/force-download
$remoteFile = 'http://chtyvo.org.ua/authors/Skriabin_Kuzma/Ya_Pobieda_i_Berlin.rtf.zip';
// file to store
$localFile = 'kuzma.zip';

$fin = fopen($remoteFile, "r");
if (!$fin) {
    die("Unable to open remote file");
}

$fout = fopen($localFile, "w");
if (!$fout) {
    die("Unable to open local file");
}

while (!feof($fin)) {
    $line = fgets($fin, 1024);
    fwrite($fout, $line, 1024);
}

fclose($fout);
fclose($fin);

Same as yours.

Sergii
Could I see your code. I have attempted both your ideas, its working but the file is always 0kb sometimes 1kb? Which is not enough!
Abs
See update. Have you tried to see raw contents of this 1kb?
Sergii
Thank you Sergii! I used your code with mine and it works perfectly. :) I have updayed my question with the code in case anyone else needs it.
Abs
Great to hear you've resolved it. But be careful with big files: you're storing whole file in memory before writing on disk.
Sergii
Just FYI ... I was experiencing a bug with IE7/IE8 (internet explorer). It would not accept the forced downloads from my app. This was over SSL. Was using the Kohana 2.3.4 download helper. Then tried the CI 1.7.2 one. No dice. The error was "server returned an invalid or unrecognized response". Anyways, Sergii's code fixed it.
Buzz