我是一位新的PHP程序員。我正在嘗試編寫代碼以允許用戶從我的網站下載文本文件。我跟蹤了關於這個問題的類似問題的答案,並將以下測試程序放在一起。它沒有強制下載文件,而是將內容發送到屏幕上(在Chrome,IE,Firefox中)。有人能指出我做錯了什麼嗎?php中的文本文件下載代碼
這裏是我的測試代碼:
<?php
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=\"test.txt\"");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
exit();
?>
測試代碼沒有顯示出來。我會再試一次: – rhou
<?php $ file =「../op/geneComp/upload/test.txt」; if(!file_exists($ file))die(「對不起,該文件似乎不存在。」); $ type = filetype($ file); //發送文件頭文件 header(「Content-type:$ type」); header(「Content-Disposition:attachment; filename = \」test.txt \「」); header(「Content-Transfer-Encoding:binary」); header('Pragma:no-cache'); header('Expires:0'); //發送文件內容。 set_time_limit(0); readfile($ file); \t exit(); \t ?> – rhou
您的問題下方有一個「編輯」鏈接。您可以單擊它並將缺少的代碼添加到問題中,而不是添加註釋。 –