Directory listing in PHP
<?
/* Put the full path to the directory you want to read, in a variable called $dir_name */
$dir_name = "/home/isgcgi/www/newsletters";
/* Create a handle, the result of opening the given directory */
$dir = opendir($dir_name);
/* Start a text block, into which you'll place the list elements (your filenames) */
$file_list = "
- \n";
- $file_name
/* Using a while statement, read all of the elements in the open directory. If the name of the file is not "." or "..", print the name in your list */
while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {
$file_list .= "
\n";
}
}
/* Finish up your bullet list */
$file_list .= "
\n";
/* Close the open directory handle and end your PHP block*/
closedir($dir);
?>
Directory Listing
Files in: <? echo "$dir_name"; ?>
<? echo "$file_list"; ?>
What am I doing wrong? I can't get the first $filename to read out, it shows as nothing, but the second $filename prints fine.
Adapting the script from http://builder.cnet.com/webbuilding/0-3882-8-4639552-5.html
Help!
Suzanne
Suzanne posted this at 08:52 — 4th January 2002.
She has: 5,507 posts
Joined: Feb 2000
while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..") && ($file_name != "index.php")) {
$file_list .= "
\n";
}
Make sure the freaking variable is right! Argh.
S
Mark Hensler posted this at 20:36 — 4th January 2002.
He has: 4,048 posts
Joined: Aug 2000
That does help a bit, doesn't it?
Wil posted this at 13:47 — 5th January 2002.
They have: 601 posts
Joined: Nov 2001
You could probably optimise that code a fair way, if you wanted to. There's no grep function in PHP?
Suzanne posted this at 19:04 — 5th January 2002.
She has: 5,507 posts
Joined: Feb 2000
How so?
I'm intrigued!
Of course, I'm also just learning (duh!) so talk in small words.
Suzanne
Mark Hensler posted this at 03:03 — 6th January 2002.
He has: 4,048 posts
Joined: Aug 2000
Yes, there are grep functions. PHP doesn't function the same way PERL does with regex. PHP requires you to put your regex in a function (whereas in perl, practically anywhere will do).
PHP Docs:
ereg(), ereg_replace(),
eregi(), eregi_replace()
LXXXIV. Regular Expression Functions (Perl-Compatible)
preg_match(), preg_match_all(), preg_replace()
You don't really need PHP or PERL to filter files...
<?php
$command = \"ls -w1 \" . $dir . \"/*.htm* | awk 'BEGIN {FS=\\\"/\\\"} {print \\$NF}'\";
exec($command, $dir_list);
foreach ($dir_list as $file) {
$file_list .= '<li><a href=\"' . $file . '\">$file</a></li>\n';
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Suzanne posted this at 03:13 — 6th January 2002.
She has: 5,507 posts
Joined: Feb 2000
I'm just starting to explore the concept, but I, just today, made an index.php page that read the contents of a directory (all different kinds of images) and wrote code around them. Since there were over 80 images, this saved me a WHACK of time. I had them write the JavaScript for the rollovers, too.
Very interesting.
I am seeing all sorts of faster development coming.
Thanks for the links, Mark, too. My goal was to learn PHP while on maternity leave, but the maternity leave isn't really happening, so I'll have to learn it on the job.
Suzanne
Mark Hensler posted this at 06:48 — 6th January 2002.
He has: 4,048 posts
Joined: Aug 2000
Doh!
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.