PHP如何遍历指定文件夹,获取所有文件列表并生成下载链接??

如题所述

试编写代码如下:

<?php

$dir="D:/WWW/ftp"; //指定的路径
$sitepath = 'http://localhost/ftp/';
//遍历文件夹下所有文件
if (false != ($handle = opendir ( $dir ))) {
    echo "$dir ç›®å½•ä¸‹çš„文件列表:<BR/>";
    $i = 0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && !is_dir($dir.'/'.$file)) {
            echo '<a href="' . $sitepath . $file . '">'.$file. '</a><br/>';
        }
    }
    //关闭句柄
    closedir($handle);
}

?>


代码中需要提示的是:


如果是运行于互联网上,需要考虑文件的访问安全性。


运行截图:


温馨提示:答案为网友推荐,仅供参考
相似回答