12

So I have a file system that looks like this:

Music
- 001
-- song.mp3
- 002
-- song.mp3
- 003
- 004
- 005
-- song.mp3
musicplayer.html
musicplayer.js

I was curious if it is possible to get a list of all of the folders names?

This javascript, html5, or even jQuery, I can not install anything.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Robert E. McIntosh
  • 5,557
  • 4
  • 26
  • 35
  • stop and think about it... if you could access any server and get directory details with javascript... how secure would server be? You can't do it – charlietfl Mar 21 '13 at 01:19

4 Answers4

14

he has a point in his question (HTML5)


I was curious if it is possible to get a list about all of the folders names? This javascript, or even jQuery, I can not install anything

Simply No , but not the Last Answer !

JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.

There are 3 major types of JavaScript namely:

Client-Side JavaScript (CSJS) -- an extended version of JavaScript that enables the enhancement and manipulation of web pages and client browsers

Server-Side JavaScript (SSJS) -- an extended version of JavaScript that enables back-end access to databases, file systems, and servers

Core JavaScript -- the base JavaScript language

Client-Side JavaScript (CSJS) and Server-Side JavaScript (SSJS) are dependent on the core JavaScript and cannot work without it.

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

There are subsets of general JavaScript — ADsafe, Secure ECMA Script (SES) — that provide greater level of security, especially on code created by third parties (such as advertisements).

I was curious if it is possible to get a list about all of the folders names? with HTML5

HTML5 provides FileSystem API which may solve your thirst , at least for know :)
Read the tutorial here : http://www.html5rocks.com/en/tutorials/file/filesystem/


another solution is to use the ugly browser Api, that i never ever recommends
Best Solution is to use a server side language like php

internals-in
  • 4,798
  • 2
  • 21
  • 38
  • Thank you for pointing out the dont vote down, I was just asking if it was possible. I would love to use php but I cant I can not install any programs. I tried using html5 api, but it seems you have to programmatically create the directories. The problem with that is that I can not know how many folders there are or what the song name is within the folder. This is starting to seem like I wont be able to accomplish this. – Robert E. McIntosh Mar 21 '13 at 01:00
  • @Robert E. McIntosh Dont use Browser Api's any way, they are not compatible with others, what is your need actually? – internals-in Mar 21 '13 at 01:05
  • I need to get a list of directories and the file name where the javascript file resides. It is for a widget I am creating from a cydia tweak called iWidgets. – Robert E. McIntosh Mar 21 '13 at 01:08
  • So basically anything that would work in safari or google chrome would be fine. – Robert E. McIntosh Mar 21 '13 at 01:09
  • "JavaScript (JS) is an interpreted computer programming language" — It isn't. It is compiled. The compile step just isn't performed manually by the code author. – Quentin Nov 18 '16 at 14:59
  • "There are 3 major types of JavaScript" — That's an oversimplification. Any JS host environment will extend the language, but there are more than two host environments including, but not limited to: JavaScript embedded in a webpage and executed by a browser, JavaScript used to write a browser extension, JavaScript running on the server in Classic ASP, JavaScript in Node.js (which could be as an HTTP server, or to write a command line utility, etc), or as a desktop application via Electron or similar, or as a plugin for Adobe products, or via Windows Scripting Host, and the list goes on. – Quentin Nov 18 '16 at 15:02
  • "Best Solution is to use a server side language like php" — That's an odd recommendation given you've already pointed out that JavaScript can be used server side. – Quentin Nov 18 '16 at 15:03
  • @RobertE.McIntosh I know it been quite some years for now but I can actually give you a function I have written in the past that is doing exactly what you were asking for... – Alex Ruhl Sep 10 '21 at 04:41
  • ` $fData) { if($fData['parentFolder'] == $GFID) { $minus = ""; $lvl = $count; while($lvl > 0) { $minus .= " - "; $lvl--; } echo $minus."".$fData['name']."
    \r"; listFolder($fId, $count + 1); } } } echo "".$folders[$user['root']]['name']."
    \r"; listFolder($user['root'], 1); ?>`
    – Alex Ruhl Sep 10 '21 at 04:54
0

Are you trying to get the client's file system? If so, web browsers usually prohibit this type of behavior for security reasons.

If you're trying to list things on your web server, this can't be done without AJAX since JavaScript runs on the client's machine. You would need to have some server code to list the directory for you, but this can pose a potential security risk.

I think your best bet is to run server side code to list the directory, it's not the safest thing to do that on client side code.

  • they would be where the html file exists. So I dont mind using ajax or anything like that, but that problem is I only have access to javascript and html. If this is impossible, that is fine, I thought I would ask at least. – Robert E. McIntosh Mar 21 '13 at 00:49
  • @RobertE.McIntosh It's impossible to do only with Javascript, you would have to add some server code (PHP or something like that) to list the contents of the Music directory and return that to you via AJAX. –  Mar 21 '13 at 00:55
  • darn. Thank you. I was worried about that. – Robert E. McIntosh Mar 21 '13 at 01:01
0

If you are asking on client side Javascript, Yes, we can access in Mozilla Firefox using File API, i don't know other browsers.

https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O

mahesh
  • 332
  • 4
  • 7
-2

You could definitely accomplish this, as @Tariq said, with some serverside code, like php. you could submit directory paths in relation to the root via ajax (check out the jquery $.post() @ http://api.jquery.com/jQuery.post/ ).

<?php
  if (isset($_POST['submit'])) {
  //capture post varibles, sanitizing of course.
  $dir = "";
  $dir = $_POST['input'];

    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
      $files[] = $filename;
    }
//now, do stuf with files

    sort($files);
    print_r($files);
  }

?>

Here's the output:

Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)

Hope this helps.

Todd
  • 5,314
  • 3
  • 28
  • 45
  • As I stated it has to be in javascript, jquery, or html. I can not install anything, so php is not available to me. Thank you though. I think this is impossible is all. – Robert E. McIntosh Mar 21 '13 at 01:03
  • [HTML5 File API](http://www.w3.org/TR/file-system-api/) -- there's some html5 goodness that may interest you. – Todd Mar 21 '13 at 01:07
  • I have been trying to use this, but it seems it doesn't actually look up the directories, unless you create them using javascript. – Robert E. McIntosh Mar 21 '13 at 01:10