"Open with" 自定义文件格式对话框

"Open with" dialog for custom file format

我有一个文件 file.magicExt,它只是一个文本文件 (utf-8),例如:

hello

我有一个 index.php:

<?php

echo '<a href="file.magicExt">file</a>';

我想要的是 在用户单击 "file" link 时显示 "open with" 对话框。

而是浏览器(Firefox,Chrome)显示文件内容。

我用这样的方法搞定了file.php:

<?php

// We'll be outputting a .magicExt
header('Content-Type: application/magicExt');

// It will be called downloaded.magicExt
header('Content-Disposition: attachment; filename="downloaded.magicExt"');

// The .magicExt source is in original.magicExt
readfile('original.magicExt');

基于PHP header() function

index.php:

echo '<a href="file.php">file</a>';