如何正确下载.vcf文件

5
我想知道如何实现Vcard下载。这是我的当前代码:
$path = "../../media/resources/";  
$file = "someName.vcf";  

header('Content-Type: text/x-vCard');  
header('Content-Disposition: attachment; filename= "'.$file.'"');  
header('Content-Length: '.filesize($path.$file));  
header('Connection: close');  

readfile($path.$file);

很遗憾,它只提供了.vcf文件中的内容。我该如何将这个vcard以下载的方式提供给用户呢?


你确定路径 $path.$file 定义的文件存在吗?打开 error_reporting(E_ALL); ini_set('display_errors', 'on'); 然后重试一下吧 ;-) - zerkms
2个回答

4

您使用了header('Connection:close');,我想关闭连接会在读取文件内容之前进行。我已将该行删除。

我不确定content-type的大小写敏感性,因此我将其更改为x-vcard,并将content-disposition更改为inline(解决IE下载问题的已知方法)。请尝试以下操作:

$path = "../../media/resources/";  
$file = "Toni_Junas.vcf";  

header('Content-Type: text/x-vcard');  
header('Content-Disposition: inline; filename= "'.$file.'"');  
header('Content-Length: '.filesize($path.$file));  

readfile($path.$file);

此外,请确保目录“resources”可读(对目录执行chmod 755命令),并且文件存在...

"text/x-vcard"现在已被弃用,建议使用"text/vcard"。(https://tools.ietf.org/id/draft-ietf-vcarddav-vcardrev-02.html) - Delmontee

0

输入 exit()

readfile($path.$file);
exit();

1
如果上述代码是整个文件(最好假设是这样的),那么不需要使用退出。 - Brendan Bullen
这个 exit() 是干什么用的?是为了 flush() 行为吗? - joksnet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接