0

我在 php 中创建了一个文件名阅读器,它从文件夹中读取文件,现在我想添加一个功能,让我大声通过 webreader 下载这个文件,然后让我选择要保存它的位置

我真的不知道脚本有什么问题。

有人有想法吗?这就是我目前所拥有的。

索引.php

<?php
$mydir = "images/";
if ($handle = opendir($mydir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
?>
           <a href="download.php?f=<?php echo $file ?>" target="_blank"><?php echo $file ?></a>
<?php
        }
    }
    closedir($handle);
}
?>

下载.php

<?php $filename = $_GET['f'];
// set this to the path where your zipped files are located
$filepath = "images" . $filename;
// a little security
if(strpos($filename, '..') !== false || realpath($filepath) != $filepath) die();
if (file_exists($filepath)){
    // make sure the browser knows what it's getting, and what to do with it
    header('Cache-Control: public');
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename=' . $filename);
    readfile($filepath);
    die(); 
} else {
    die('Error: File not found.');
}

?>
4

1 回答 1

0

尝试$filepath = "images/" . $filename;代替$filepath = "images" . $filename;

于 2012-05-12T10:17:43.697 回答