user215645
user215645

Reputation:

Open web directory to simple webpage

This is a fairly simple problem, I'm just not sure exactly how to approach it.

A friend of mine has an open directory on his webserver, and he asked me to make a simple webpage for him which would just display all of the file names, rather than the dates/file sizes/etc. The page would also update when a new file is added into the directory.

So, I guess I'm just looking to be pointed in the right direction on this one. My first guess was just to throw a simple HTML/Javascript page together which would extract all of the file names from the directory with Javascript, then display them on the webpage while linking to the file. Or am I going able this all wrong?

Thanks,
aqzman

Upvotes: 1

Views: 511

Answers (3)

casraf
casraf

Reputation: 21694

You can do this pretty easily with PHP -

$files = scandir($_GET['dir']);
foreach ($files as $file) {
    if (is_dir($_GET['dir']))
        echo '<a href="?dir='.$_GET['dir'].'/'.$file.'">'.$file.'</a><br />';
    else
        echo '<a href="'$_GET['dir'].'/'.$file.'">'.$file.'</a><br />';
}

Upvotes: 0

aefxx
aefxx

Reputation: 25249

You could use Apache's built-in directory listing feature. With javascript this can't really be done (exception: there's a pattern within the filenames that would let you send HEAD requests to see if files exist - see this site where I had to use this technique).

Upvotes: 1

Andy E
Andy E

Reputation: 344567

JavaScript is a client-side language and really has no way of enumerating files and directories on a web server, without the aid of a server-side script anyway. You need to look into server-side scripting languages such as Python, PHP, and ASP.NET (Windows Server only), to name a few.

These languages are processed on the server and make changes to (or even create from scratch) a page before it is sent to the client/browser.

Upvotes: 1

Related Questions