Is there a way that i can get all the files and directories on the server using js? Lets say there is a folder on the server called Files, inside the files folder, there are other folders and files but not a set value and can change constantly. Is there a way to scan the Files folder?
-
1possible duplicate of Javascript to list all files in directory on the webserverPhilipp– Philipp2014-11-09 16:42:57 +00:00Commented Nov 9, 2014 at 16:42
-
2Not in JS, you need to do this on a server using some server-side language.Teemu– Teemu2014-11-09 16:43:04 +00:00Commented Nov 9, 2014 at 16:43
2 Answers
you can achieve this wiht a server side script, for example PHP http://php.net/manual/en/function.scandir.php
this function returns the dirs, you could nested to get all the dirs and files in directories then you can return to the javasript with
<? echo json_ecnode($array_with_dirs);?>
with an ajax Request
Comments
If you are asking about client-side JavaScript only, no, you cannot do that. You can read URLs using Ajax. If a URL corresponds to a file, you've read the file. If a URL corresponds to a directory and the server responds server-generated index, you could parse that index and recursively read files and indices. The "crawler" programs used by, e.g. Google, employ a similar technique, but do not depend on server-generated indices; they just follow links.
If the files you ask about are not accessible to the web server program, i.e. outside the server's document root, then you cannot read them using only client-side code.