下面的代碼強制瀏覽器發出提示保存/打開文件(https://kb.wisc.edu/images/group27/13334/open-prompt.PNG),即使它是一個圖像或PDF文件。php如何提供文件,但不能下載
我想讓圖像像往常一樣打開,pdf文件要在瀏覽器中顯示。當然其他不受瀏覽器支持的文件如zip,rar,doc,xls等將激發保存文件對話框。
編輯: 我的目的不是阻止客戶端保存文件,當然他們可以保存它是不可能的。我想讓服務讓圖像像PHP這樣的文件,如main.php?file = randomcode(存儲在數據庫中),而不是/images/somefilename.jpg。我的代碼強制客戶端下載它,但我想顯示它。
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "." . $fileinfo["file_extension"] . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
$fp = fopen($file, "r");
if ($fp) {
while (!feof($fp)) {
$cur_data = fread($fp, 1024);
echo $cur_data;
}
} else {
echo "Error: Could not the read file.";
}
你曾經在互聯網上放過的每件東西都會下載,如果它到達網絡瀏覽器的話。你的**實際**問題是什麼? – Amelia
這段代碼強制瀏覽器發出提示保存/打開文件(https://kb.wisc.edu/images/group27/13334/open-prompt.PNG),即使它是圖像或pdf(當然這需要Adobe Reader插入)。我想要像往常一樣打開圖像,在瀏覽器中顯示pdf文件。當然其他不受瀏覽器支持的文件如zip,rar,doc,xls等將激發保存文件對話框。 – Ergec