So what I want to do is to retrieve all the file names from a local directory using JavaScript and then populate these filenames in html form with separate checkboxes for each filename, Kindly help. A sample code for this task would be appreciated.
Asked
Active
Viewed 164 times
0
-
Does this answer your question? [Get list of filenames in folder with Javascript](https://stackoverflow.com/questions/31274329/get-list-of-filenames-in-folder-with-javascript) – Heretic Monkey Mar 16 '21 at 12:29
2 Answers
0
From browser No, client side Javascript has no access to filesystem

Bharath S
- 364
- 4
- 10
-
This used to be true, but it is now possible to access the local filesystem from client-side JavaScript using the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API). – Anderson Green Feb 23 '22 at 21:47
-
So when using FILE System API user must select a folder and give access to read the files? @AndersonGreen – Bharath S Apr 04 '22 at 12:46
-
Yes, the API allows client-side JavaScript to read or modify directories that are selected by the user. – Anderson Green Apr 05 '22 at 03:46
-1
You can do it using ajax. Send an ajax request to a php file, there retrieve filenames using php and send it as ajax response. You'll get it.
JS code
$.ajax({
url : URL_TO_PHP_FILE,
type : "get",
success:function(res){
console.log(res)
}
});
PHP code
$dir = "/images/";
$a = scandir($dir);
/*
Sample output of $a
Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
)
*/
echo json_encode($a);
exit;

Majid Ali
- 109
- 6
-
If its not much of an issue, Can you share a sample code for this task which might help because I was searching for the solution in JavaScript because of not much familiarity with these two. – Syed Ismail Shah Mar 16 '21 at 17:57