【问题标题】:How to download a .vcf file correctly如何正确下载 .vcf 文件
【发布时间】:2011-05-26 14:34:05
【问题描述】:

我想知道,如何实现 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'); 并重复 ;-)

标签: php download vcf-vcard


【解决方案1】:

您有header('Connection: close');,我想它会在读取文件内容之前关闭连接。我已经删除了这条线。

我不确定内容类型是否区分大小写,因此我将其更改为 x-vcard,并将内容处置更改为内联(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)并且文件存在...

【讨论】:

【解决方案2】:

输入exit()

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

【讨论】:

  • 如果上面指定的代码是整个文件(最好假设是),则不需要退出
  • 这个exit() 是为了什么?这是为了flush() 行为?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2018-11-17
  • 2021-05-05
  • 1970-01-01
相关资源
最近更新 更多