保存文件夹 PHP 中的文件


save files from folder PHP

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

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

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

索引.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.');
}
?>

尝试$filepath = "images/" . $filename;而不是$filepath = "images" . $filename;