我已将 .pdf、.doc、.txt 文件存储在 Web 服务器的目录中。我能够显示存储在目录中的所有文件,但下载失败。每次我点击超链接 sample.txt 都会下载空白内容。请查看代码并建议我。
这就是我在 show_directory.php 中使用的
<?PHP
// Define the full path to your folder from root
$path = "/public_html/bc/upload/";
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");
// Loop through the files
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "download_file.php" )
continue;
echo "<a href=\"download_file.php\">$file</a><br />";
}
// Close
closedir($dir_handle);
?>
download_file.php 的内容为:
<?PHP
//download.php
//content type
header('Content-type: text/plain');
//open/save dialog box
header('Content-Disposition: attachment; filename="sample.txt"');
//read from server and write to buffer
readfile('test.txt');
?>