1

我已将 .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');
?>
4

1 回答 1

0
....
header('Content-Description: File Transfer');
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . 'test.txt' . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('test.txt');
....

为什么不使用二进制?

于 2012-09-18T07:03:55.417 回答