The easiest way to get a list of files in a server directory

I need to get an array of all images (or just all files) in a directory (e.g. www.example.com/images/). I prefer to use JavaScript, but it's hard to do. So should I use PHP, meybe?

Could you help me? I do not understand this.

Many thanks!

+5
source share
2 answers

Javascript cannot retrieve all the files on the server, as it is a client interface.

http://php.net/manual/en/function.glob.php is what you need.

$all = glob('/path/to/dir/*.*'); $images = glob('/path/to/dir/*.{jpg,png,gif}'); 
+3
source

I do not agree with @mariobgr. If there are no server settings prohibiting the directory listing, then the html generated by querying this directory can be parsed for content.

 $ tree maindir maindir ├── index.html └── somedir ├── doc1 ├── doc2 └── doc3 

index.html

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"><!-- JQUERY --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> <title></title> </head> <body> <h1>Listing /somedir</h1><!-- Custom Script (defered load, after dom ready) --> <script> $.get('http://localhost/maindir/somedir', (data) => { console.log(data); let listing = parseDirectoryListing(data); $('body').append(JSON.stringify(listing)); }); function parseDirectoryListing(text) { let docs = text .match(/href="([\w]+)/g) // pull out the hrefs .map((x) => x.replace('href="', '')); // clean up console.log(docs); return docs; } </script> </body> 

A visit to localhost/maindir calls:

Listing / somedir
["Doc1", "doc2", "doc3"]

0
source

All Articles