How can i make a php script which will automatically generate links of my folder's file

Anik Hasan

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...

Michael Eugene Yuen

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Which file(s) should I include in my php script to make use of functions WordPress provides?

From Dev

How do I determine which file(s) generate which dependencies for my rpm build?

From Dev

Can I automatically generate a change script using a .scmp file?

From Dev

How can I use the contents of a PHP file in my script?

From Dev

How can I make my Python program's executable file?

From Dev

How can i make page links which don't redirect

From Dev

How can I make responsive html table fields which are automatically filled in by user's input in an html form?

From Dev

How can I find out which program creates a file/folder in my C: drive?

From Dev

How can I make my framework's code automatically run when the app launches?

From Dev

How can I make an autorun to make a program open a file automatically

From Dev

How can I make symbolic links in one folder for multiple files in another folder

From Dev

How can I include PHP files if the file that I'm going to include is located outside the folder of my current PHP file

From Dev

How I can redirect my site links via an .htaccess file?

From Dev

How can I open a folder in Atom editor directly from within my file manager's context menu?

From Dev

How can I make Firefox annotate links which point to files such as PDFs, which won't be opened in Firefox?

From Dev

How can I make my computer do a repeated series of tasks by a script file on Windows?

From Dev

How can I make my PHP script log the IP of all visiters?

From Dev

How can I make two files open automatically in a .bat file?

From Dev

How can i read the db in my android app which i've modified in my pc and copied back to my app's db folder

From Dev

How can I make a zip file of a directory except specific folder of it?

From Dev

Php beginners questions, how can I exclude a folder in this script

From Dev

How can I generate a PHP array and then insert it into a PHP file?

From Dev

How can I connect my python script with my HTML file?

From Dev

How can I dynamically make folder with today's date?

From Dev

How can I dynamically make folder with today's date?

From Dev

How can I write a script which updates a text file?

From Dev

How can I make my Python script faster?

From Dev

How can i make my script wait before continuing?

From Dev

How can I make my batch script add code to itself

Related Related

  1. 1

    Which file(s) should I include in my php script to make use of functions WordPress provides?

  2. 2

    How do I determine which file(s) generate which dependencies for my rpm build?

  3. 3

    Can I automatically generate a change script using a .scmp file?

  4. 4

    How can I use the contents of a PHP file in my script?

  5. 5

    How can I make my Python program's executable file?

  6. 6

    How can i make page links which don't redirect

  7. 7

    How can I make responsive html table fields which are automatically filled in by user's input in an html form?

  8. 8

    How can I find out which program creates a file/folder in my C: drive?

  9. 9

    How can I make my framework's code automatically run when the app launches?

  10. 10

    How can I make an autorun to make a program open a file automatically

  11. 11

    How can I make symbolic links in one folder for multiple files in another folder

  12. 12

    How can I include PHP files if the file that I'm going to include is located outside the folder of my current PHP file

  13. 13

    How I can redirect my site links via an .htaccess file?

  14. 14

    How can I open a folder in Atom editor directly from within my file manager's context menu?

  15. 15

    How can I make Firefox annotate links which point to files such as PDFs, which won't be opened in Firefox?

  16. 16

    How can I make my computer do a repeated series of tasks by a script file on Windows?

  17. 17

    How can I make my PHP script log the IP of all visiters?

  18. 18

    How can I make two files open automatically in a .bat file?

  19. 19

    How can i read the db in my android app which i've modified in my pc and copied back to my app's db folder

  20. 20

    How can I make a zip file of a directory except specific folder of it?

  21. 21

    Php beginners questions, how can I exclude a folder in this script

  22. 22

    How can I generate a PHP array and then insert it into a PHP file?

  23. 23

    How can I connect my python script with my HTML file?

  24. 24

    How can I dynamically make folder with today's date?

  25. 25

    How can I dynamically make folder with today's date?

  26. 26

    How can I write a script which updates a text file?

  27. 27

    How can I make my Python script faster?

  28. 28

    How can i make my script wait before continuing?

  29. 29

    How can I make my batch script add code to itself

HotTag

Archive