在PHP中使用Header作为下载链接 [英] Using Header as download link in PHP
问题描述
function showDownloadWithHeader($ link){
echo< form action ='download.php 'method ='post'>;
echo< input class ='downloadButton'type ='submit'value ='Download Report'>;
echo< input type ='hidden'name ='filename'value ='$ link'>;
回声< / form>;
}
而在download.php中,我只希望用户被要求下载文件。
<?php
if($ _ POST ['filename'] ==''| | empty($ _ POST ['filename'])){
exit;
}
$ filename = $ _POST ['filename']; //文件是2个文件夹。例如data / stack / bla.xlsx
$ file = $ filename;
$ extension = end(explode('。',$ filename));
error_reporting(E_ALL);
ini_set(display_errors,1);
// echo $ filename;
// echo< br />;
// echo $ extension;
// echo文件大小($ filename);
// echo< br />;
switch($ extension){
case'xls':
$ mimeType ='application / vnd.ms-excel';
休息;
case'xlsx':
$ mimeType ='application / vnd.openxmlformats-officedocument.spreadsheetml.sheet';
休息;
}
// echo $ mimeType;
header('Content-Description:File Transfer');
header('Content-Type:'。$ mimeType);
header('Content-Disposition:attachment; filename ='。basename($ file));
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:must-revalidate,post-check = 0,pre-check = 0');
header('Pragma:public');
header('Content-Length:'。filesize($ file));
ob_clean();
flush();
readfile($ filename);
出口;
?>
我在php.net的readfile()函数下看到了这个解决方案,但它似乎并不适用于我。
类似这样的工作正常。
标题(Pragma:public,true);
header(Expires:0); //设置过期时间
header(Cache-Control:must-revalidate,post-check = 0,pre-check = 0);
header(Content-Type:application / force-download);
header(Content-Type:application / octet-stream);
header(Content-Type:application / download);
header(Content-Disposition:attachment; filename =。basename($ file));
头(Content-Transfer-Encoding:binary);
header(Content-Length:.filesize($ file));
die(file_get_contents($ file));
So I am having this problem wit my download link. Basically my previous way of creating download link is to just have a form button with method='link' and the action to be the link to the file. This works for firefox and others but not safari. For some reason, when the user tries to download the file (excel file) from safari, it would just show bunch of ascii characters on the browser (I guess its trying to read it using the browser?). Well, I was looking for another solution and it seems like using header is the way to do it. So now I am tryng to create a form button with method='post' and action='download.php' where there is a hidden field with the link to the file. It looks like this
function showDownloadWithHeader($link){
echo "<form action='download.php' method='post' >";
echo "<input class='downloadButton' type='submit' value='Download Report'>";
echo "<input type='hidden' name='filename' value='$link'>";
echo "</form>";
}
And inside the download.php I would just want the user to be asked to download the file.
<?php
if($_POST['filename'] == '' || empty($_POST['filename'])){
exit;
}
$filename = $_POST['filename']; //the file is 2 folder down. e.g. data/stack/bla.xlsx
$file = $filename;
$extension = end(explode('.', $filename));
error_reporting(E_ALL);
ini_set("display_errors",1);
// echo $filename;
// echo "<br/>";
// echo $extension;
// echo filesize($filename);
// echo "<br/>";
switch($extension){
case 'xls':
$mimeType = 'application/vnd.ms-excel';
break;
case 'xlsx':
$mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
}
// echo $mimeType;
header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeType);
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($filename);
exit;
?>
I saw this solution on php.net under readfile() function but it doesnt seem to be working for me. I am doing this on localhost.
Something like this works fine.
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));
这篇关于在PHP中使用Header作为下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!