PDA

View Full Version : returning a list of all files in a directory


joh6nn
10-17-2002, 11:27 PM
i'm completely new to php, so you'll have to use small words, and go slow, but what i want to do is have php return a javascript array containing a list of all the files in a directory. something along the lines of this:

<script src="php/dirlist.php?dir=/path/to/dir"></script>

and it would return something like:

var dirlist = ["afile.htm","bfile.htm","bcfile,htm","cfile.htm"];

Spookster
10-18-2002, 12:13 AM
Here are some sample scripts:

http://www.php.net/manual/en/ref.dir.php

I'm not sure why you would need to put that into a javascript array but in any case you can just run a for loop in php to echo out all the elements in the php array into the javascript array. Something like:

<?php

echo "<script>";
echo "yak = new Array(";

for($i = 0; $i<11; $i++){
echo "\"array[$i]\";
}

echo ")";
echo "</script>";

?>

joh6nn
10-18-2002, 12:58 AM
i'm gonna be using it to dynamically populate a select box after the page loads. the page you linked me too had interesting examples, but nothing that i could specifically use. i'm trying to fiddle with one of them a bit, but i'm not real sure i'm doing it right. what i got right now is this:

<?
$dir_name = "d:\php\examples";
$dir = opendir($dir_name);

while ($file_name = readdir($dir)) {
if (($file_name != "." && $file_name != "..")) {

print $file_name."
";
}
}
closedir($dir)
?>

but i don't know how to pass variables to this, and i'm not real sure how to create headers, which ( stop me if i'm wrong ) i need to do in order to return javascript content.

mordred
10-18-2002, 03:36 PM
Accoring to your first example, you need to grab the GET parameters that come to this PHP script. There is a super-global associative array that goes by the name of $_GET. Super-global means that you don't have to use "global" to import this array into the scope of a function (i.e. it's visible everywhere).

So it might be something along this line:

$dir_name = $_GET['dir'];

Double-check the incoming values in order to prevent a malicious user from abusing this functionality (e.g. like passing "../../secret/directory" to your script).

joh6nn
10-18-2002, 10:35 PM
good point. how would i go about doing that? i'm thinking that i should make sure that the parameter passed doesn't contain a restricted directory name, by using something like javascript's String.indexOf(), but i'm completely new to php, so i wouldn't know what method or function i'm looking for, or how to use it.

joh6nn
11-09-2002, 05:55 AM
ok, i still need a some help, i think.

this is what i've gotten to so far:

<?php
$dir_name = $_GET['dir'];
$dir = opendir($dir_name);

// use strstr to check input
// currently no directories to filter out, so, no strstr.

header("Content-type: text/javascript"); // i'm trying to return a .js file

print "var dirList = new Array();

while ($file_name = readdir($dir)) {
if (($file_name != "." && $file_name != "..")) {
print "dirList[dirList.length] = $dir_name / $file_name";
}
}
closedir($dir)

print "dirList.sort();"
?>

i'm not sure if i'm missing something as far as returning the content type.

mordred
11-09-2002, 02:18 PM
A couple of minor mistakes like missing semicolons and quotes, but after fixing them, it worked for me in Mozilla:


$dir_name = $_GET['dir'];
$dir = opendir($dir_name);

// use strstr to check input
// currently no directories to filter out, so, no strstr.

header("Content-type: text/javascript"); // i'm trying to return a .js file

print "var dirList = new Array();\n";

while ($file_name = readdir($dir)) {
if (($file_name != "." && $file_name != "..")) {
print "dirList[dirList.length] = '$dir_name / $file_name';\n";
}
}
closedir($dir);

print "dirList.sort();\n";
print "alert(dirList.join('\\\\n'));";

joh6nn
11-10-2002, 02:00 PM
ok, thanks for the help Mordred.

ConfusedOfLife
11-11-2002, 08:46 AM
hey mordred, I have some few questions! :D


Instead of using that super global _GET, can't we just use HTTP_GET_VARS??
Considering that "header("Content-type: text/javascript");", can't we simply print "<script language='javascript'>" out? What's the difference? And if HTTP makes a .js file, then how can we put html in it too?
Why did you print out the dir.sort()? We already had our js array.
How can you write your code such beautifully?! You don't choose color for all your paranthesis, brackets, comments and ..., do you?!

mordred
11-11-2002, 10:56 AM
Originally posted by ConfusedOfLife
hey mordred, I have some few questions! :D


let's see...


Instead of using that super global _GET, can't we just use HTTP_GET_VARS??


If you need to make your script very backwards-compatible (PHP < 4.1), do so, they work the same only that HTTP_GET_VARS is not super-global, so to access it from a function, class etc. you need to pass it as a parameter or use the "global keyword".


Considering that "header("Content-type: text/javascript");", can't we simply print "<script language='javascript'>" out? What's the difference? And if HTTP makes a .js file, then how can we put html in it too?


I thought joh6nn was trying to build an external .js file dynamically on server-side. An external javascript file must not have anything else than pure JS inside. Does that answer your question? Also, it's not HTTP that makes a .js file, it's PHP. HTTP is just the protocol client and browser use to talk with each other.


Why did you print out the dir.sort()? We already had our js array.


That was taken from joh6nns code example. The order in which files are read by readdir() is systems dependent and not necessarily ordered by alphabet. To get a sorted js array, including this function makes sense.


How can you write your code such beautifully?! You don't choose color for all your paranthesis, brackets, comments and ..., do you?!


Of course I do. I have a lot of pencils right here at my desk and plenty of time :D. No, seriously, I do only use the [php] formatting tags of this forum software, they seem to use PHPs highlight_string() function.

ConfusedOfLife
11-11-2002, 04:38 PM
Thanx, I almost got all my answers, but about this HTTP! I don't know much ( anything! ) about it, any link or article that I can learn or just continuing my studies on PHP does the trick?! ( Should I know how to network and these stuff to be a good server programmer? I really hate networking! )

mordred
11-11-2002, 05:42 PM
Some basic knowledge of HTTP is just helpful for developing web applications, because it is the underlying protocol. For PHP, it's nice to know what you can send in a header() function and what effect it has. Surely PHP is a lot more, but the more you script in PHP, the more familiar you will get with HTTP terms.

If you enjoy reading boring specs, have a look at http://www.w3.org/Protocols/

joh6nn
11-23-2002, 02:58 AM
ok, it works for me, and that's good, but it only takes directories as they're named on the server; it won't parse urls for me.

that is, i have to use dirlist.php?dir=/home/MyAccountName/public_html . i can't use dirlist.php?dir=http://www.joh6nn.com .

how would i go getting this to accept urls?