保存推送头文件以强制下载的远程文件 [英] Save remote file that pushes headers to force download
问题描述
我可以使用PHP下载远程文件,但是如何从一个将头插入的链接下载?我的意思是,您可以点击一些链接,它将强制下载并提供对话框来保存文件。如何使用PHP 下载并保存这样的东西?
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.
感谢您的帮助
<?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);
?>
推荐答案
只是试图重现情况。 Gubmo是对的,这种下载方式适用于我的内容类型:应用程序/八位字节流和内容类型:应用程序/强制下载标题。
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.
如 here 所述HTTP 410意味着客户端请求的URL不再可用于该系统。这不是一个从未听说过的回应,而是不再在这里生活的回应。也许他们有某种反垄断制度。
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.
应该进行调查。如果他们需要Cookie - stream-context-create 可以帮忙或者也许他们检查引用。但是我几乎肯定问题不在标题中。
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.
希望这有帮助。
UPD 您要询问的示例代码。
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);
与您的相同。
这篇关于保存推送头文件以强制下载的远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!