如何制作一个 php 脚本,该脚本将自动生成我文件夹文件的链接



我想为我的网站制作一个脚本,它会自动生成链接, 我会把我的所有文件放在一个文件夹中,我希望这些文件将显示在网页中并且可以下载。 请帮忙...

您可以使用 GLOB() 按字母顺序为目录中的所有文件创建一个数组,然后使用 foreach() 循环将它们回显出来:

<?php
$folder = 'download/';
$files = GLOB($folder . '*{.*}', GLOB_BRACE);
foreach ($files as $file) {
echo '<a href="'.$file.'" download>'.basename($file).'</a>';
}
?>

如果您只想列出具有特定扩展名的文件:

<?php
$folder = 'download/';
$file_types = array(
'doc',
'pdf',
'txt'
);
$files = GLOB($folder . '*{.' . implode(',.',$file_types) . '}', GLOB_BRACE);
foreach ($files as $file) {
echo '<a href="'.$file.'" download>'.basename($file).'</a>';
}
?>

将它们放在无序列表中:

$files = GLOB($folder . '*{.' . implode(',.',$file_types) . '}', GLOB_BRACE);
echo '<ul>';
foreach ($files as $file) {
echo '<li><a href="'.$file.'" download>'.basename($file).'</a></li>';
}
echo '<ul>';

ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
padding: 8px 15px;
float: left;
margin-right: 15px;
width: 200px;
text-align: center;
color: #333;
}
li:nth-child(odd) {
background-color: #e6e6e6;
}
li:nth-child(even) {
background-color: #f2f2f2;
}
<h2>Download Folder Listing</h2>
<ul>
<li><a href="file1.txt" download>file1.txt</a></li>
<li><a href="file2.txt" download>file2.txt</a></li>
<li><a href="file3.txt" download>file3.txt</a></li>
<li><a href="file4.txt" download>file4.txt</a></li>
<li><a href="file5.txt" download>file5.txt</a></li>
</ul>

我不确定你真正想要的是什么。

但如果只是为了您的使用:

安装阿帕奇服务器

将所有文件放在 Apache 内部的一个文件夹中

不要在此文件夹中放置任何索引.html

转到 http://localhost/nameOfYourFolder

如果您不尝试某些东西并提出真正的问题,我们无法为您提供更多帮助

最新更新