I want to make a script for my website which will automatically generate links, i will put all my files in a folder and i want those files will show in a web page and can be downloadable. please help...
You can use GLOB() to create an array for all the files from directory in alphabetical order then echo them out with a foreach() loop:
<?php
$folder = 'download/';
$files = GLOB($folder . '*{.*}', GLOB_BRACE);
foreach ($files as $file) {
echo '<a href="'.$file.'" download>'.basename($file).'</a>';
}
?>
If you just want to list files with specific extensions:
<?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>';
}
?>
To put them in an unordered-list:
$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>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments