0

A pure newbie in javascript :)

What I need is a function to get all the file names in a directory(lets call it results) which is in the same place with my html file.

The function could return array that holds file names or the result could be stored in a global variable.

Regards!

Edit Thanks everyone for the answers and information. I really did not know that I had to use a client side language :)

6
  • which server side language are you using? Commented Jul 27, 2018 at 12:48
  • @TAHASULTANTEMURI I'm only using html and javascript. I only have 1 .html file. Everything is done in it. Commented Jul 27, 2018 at 12:49
  • Possible duplicate of Get list of filenames in folder with Javascript Commented Jul 27, 2018 at 12:50
  • 3
    You can't do that client sided because JS has no access to the file system. You would need server sided processing Commented Jul 27, 2018 at 12:51
  • @Un_NatMenDim client side script cannot read the files inside server directories ,this is security issue ,so you need any server side language to make it done. Commented Jul 27, 2018 at 12:52

2 Answers 2

1

I saw a node answer; here's what you can do in PHP;

Create a new file called 'files.php' and put the following code in there:

<?php
    // Specify the directory to scan for files
    $dir    = '/';
    // Store the scandir results in a variable
    $files = scandir($dir);
    // Encode the array in JSON and echo it
    echo json_encode($files);
?>

Save it and upload it to the directory you want to return the files from, make sure you specify the file path (you can modify the $dir with the path needed);

Now you can use JQuery for example to create a GET request to the files.php file we created and return the data back to the client,

<script>
    $.get( "files.php", function( data ) {
          console.log(data);
        });
</script>

You'll see in the console the returned JSON containing all the file names from the result of the scandir; you can use these now to push into your UI, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

What you are trying to do is accessing the directory using JavaScript. Probably you have done so in a server-side language before (Java, Python etc..).

But this is not possible in JavaScript or better to say JavaScript doesn't allow this.

The reason for this is simple. The Javascript runs on your browser and just imagine if I can write a code that runs on your browser and accesses your directories. I could literally read everything on your computer.

Its absurd. Isn't it ? So its simply not possible.

Update: While writing the above answer, I didn't account for server side javascript using Node.js and only focused on javascript on browser. In case, you are using javascript on the server-side then YES, the FileReader module from Node should allow you that.

Though its a 2 year old answer but still Thanks @SQB for pointing that out.

1 Comment

What about server-side javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.