Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to get list of files in the server directory

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

Could you please help me - I'm not good at this.

Thank you very much!

like image 257
Jan Chalupa Avatar asked Jun 03 '15 14:06

Jan Chalupa


1 Answers

I managed to do it in base javascript using a modifyed ajax command to get the folder list as an html file. Then I grep the file names from inside of it:

function loadDoc() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    
                    // gets the entire html file of the folder 'logpost' in this case and labels it thing
                    thing = this.responseText
                    searchFor = /.html</g
                    a=0;
                    b=0;
                    var str = "";
            
                    // greps file for .html and then backs up leter by letter till you hot the file name and all
                    while ((dothtmls = searchFor.exec(thing)) != null ){

                        str = "";
                        console.log(dothtmls.index);
                        
                        a = dothtmls.index;

                        while (thing[a]  != '>' ){
                            a--;
                        }
                        a++;
                        while(thing[a] != '<'){
                            str = str + thing[a];
                            a++;
                        }
                        console.log(str);
                    } 
                }
            };
            xhttp.open("GET", "logpost/", true);
            xhttp.send();
            }

This is probably not the cleanist way but if you are working on a static web sever it should work :)

like image 69
WILL fill Avatar answered Sep 21 '22 14:09

WILL fill