在PHP中使用Header作为下载链接 [英] Using Header as download link in PHP

查看:82
本文介绍了在PHP中使用Header作为下载链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个问题,我的下载链接。基本上我以前的创建下载链接的方法是只有一个form ='link'的表单按钮,并且该动作是文件的链接。这适用于firefox和其他人,但不适用于safari。出于某种原因,当用户试图从safari下载文件(excel文件)时,它只会在浏览器上显示一堆ascii字符(我猜它试图用浏览器读取它)。那么,我正在寻找另一种解决方案,它似乎使用头是做到这一点的方式。所以现在我尝试创建一个form ='post'和action ='download.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆