一種方法是強制瀏覽器顯示「下載文件」-dialog。因此用戶可以決定如何處理文件。
這可以通過PHP頭來完成。 (http://www.php.net/manual/en/function.header.php#83384)
也有關於如何這個(帖子83384)爲例:
<?php
// downloading a file
$filename = $_GET['path'];
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");
/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
@readfile($filename);
exit(0);
?>
使用此示例時,請考慮使用
$filename = $_GET['path'];
是一個很大的安全問題。你應該使用類似ID的東西來代替或者驗證輸入。 例如:
if($_GET['file'] == 1) {
$filename = foobar.pdf;
} elseif($_GET['file'] == 2) {
$filename = foo.pdf;
} else {
die();
}
謝謝。我很被拿走,我沒有想到做一個'readfile'調用。 – Nirmal 2010-01-04 04:57:46
是的,除非Illustrator文件以Illustrator格式* .ai *保存,否則這是最佳解決方案。 – 2015-05-18 12:29:08