visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: listing directory and filtering


By: PHP newbee U.S.A.  Date: 26/03/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Hi:

This is real easy for some but need help with this script. As it is now the script list all html files in a directory so it filters out all files except files ending in .html. I need the script to preform a couple more function and need help.

1) It needs to display just the name of the file without the extension (example: index.html would be diplayed as index but the link will point to index.html

2.) Filter out any .html files that have -days.html or -months.html and exclude those from the printed list (example: test-days.html or test-months.html)

Thanks in advance for anyones help.

Here is the code:

<?
echo "Click on customer to view transfer stats
";
$handle = opendir('.');

closedir($handle);
$handle=opendir('.');
$counter = 0;
while ($file = readdir($handle))
{
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);
if ($file != "." and $file != ".." and $allow_file)
{
$myfiles[$counter] = $file;
echo "
<a href=\"$file\">$file</a>\n";
$counter++;
}
}
closedir($handle);
?>

By: VGR Date: 26/03/2003 05:49:00 English  Type : Comment
<?
echo "Click on customer to view transfer stats
";
$handle = opendir('.');

closedir($handle);
$handle=opendir('.');
$counter = 0;
while ($file = readdir($handle))
{
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);
if ($file != "." and $file != ".." and $allow_file)
{
// get name without extension
// i don't like this : $filename = current(explode('.', basename($file)));
// I prefer good ol' style
if ($a=strpos($file,'.')) {
$ext=substr($file,$a); // . inside
$filename=substr($file,0,$a);
} else { $filename=$file; $ext=""; }
// test for presence of -days, -month
$ok=(! (strpos($filename,'-month')===FALSE));
$ok=($ok AND (! (strpos($filename,'-day')===FALSE)));
// if ok, memorize and print
if ($ok) {
$myfiles[$counter] = $file;
// printout
echo "
<a href=\"$filename$ext\">$filename</a>\n";
$counter++;
} // if ok
}
}
closedir($handle);
?>


By: PHP newbee Date: 26/03/2003 06:17:00 English  Type : Comment
I tried it and filters out everything and does not display any files.

I have test.html test-days.html and test-months.html in the same directory

It should show test on the output with a link to test.html but instead it shows nothing. If you could give me a idea of what wrong that would be great.
By: VGR Date: 26/03/2003 06:26:00 English  Type : Comment
sorry, I miswrote the tests

suppress the NOT (!)

I'm stupid :D


By: VGR Date: 26/03/2003 06:30:00 English  Type : Comment
BTW, Note that I'm not responsible for this :
$the_type = strrchr($file, ".");
$allow_file = eregi("html",$the_type);

I would never have written this that way just to extract the file extension and compare it to '.html' ;-)
By: PHP newbee Date: 26/03/2003 06:30:00 English  Type : Comment
I tried it and filters out everything and does not display any files.

I have test.html test-days.html and test-months.html in the same directory

It should show test on the output with a link to test.html but instead it shows nothing. If you could give me a idea of what wrong that would be great.
By: VGR Date: 26/03/2003 06:31:00 English  Type : Comment
You may also be interested by a question justet answered yesterday evening and that should be easily visible in the "last questions answered" ;-)

<?php

$directory = "./";
if($dir_handle = opendir($directory)){
while($file = readdir($dir_handle))
if (! is_dir($file)) // candidate ok : file
if (($file <> '.') AND ($file <> '..')) { // file OK
// get name without extension
// i don't like this : $filename = current(explode('.', basename($file)));
// I prefer good ol' style
if ($a=strpos($file,'.')) {
$ext=substr($file,$a); // . inside
$filename=substr($file,0,$a);
} else { $filename=$file; $ext=""; }
// printout
echo "<a href=\"$directory$filename$ext\">$filename</a>
";
} // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
By: PHP newbee Date: 26/03/2003 06:35:00 English  Type : Comment
Sorry but what do you mean by supressing the NOT (!)
Just trying to learn and understand. This is new to mean.
By: PHP newbee Date: 26/03/2003 06:42:00 English  Type : Comment
Ok the last one works fine but it show files with -days and -months but everything else is working perfect.
By: VGR Date: 26/03/2003 06:52:00 English  Type : Answer
of course, it wasn't adapted to your specific needs. Also note it skips directories

Here's a modified one for your problem :

<?php

$directory = "./";
if($dir_handle = opendir($directory)){
while($file = readdir($dir_handle))
if (! is_dir($file)) // candidate ok : file
if (($file <> '.') AND ($file <> '..')) { // file OK
// get name without extension
// i don't like this : $filename = current(explode('.', basename($file)));
// I prefer good ol' style
if ($a=strpos($file,'.')) {
$ext=substr($file,$a); // . inside
$filename=substr($file,0,$a);
} else { $filename=$file; $ext=""; }
// test for presence of -days, -month
$ok=(strpos($filename,'-month')===FALSE);
$ok=($ok AND (strpos($filename,'-day')===FALSE));
// if ok, memorize and print
if ($ok) {
// printout
echo "<a href=\"$directory$filename$ext\">$filename</a>
";
} // if file not days nor month(e)s
} // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
By: VGR Date: 26/03/2003 06:54:00 English  Type : Comment
in the " if ($ok) { " just add :

$myfiles[$counter++] = $filename; // without extension ; $file is full path

and you are done with it.

By: PHP newbee Date: 26/03/2003 06:59:00 English  Type : Comment
Works great, thank you for your time and I understand
how it all works now.
By: VGR Date: 26/03/2003 19:03:00 English  Type : Comment
of course you understand, because I didn't use ereg_replace(), strrchr() and such fancy things ;-)

Sorry for the time to reach a proper solution : I tried to adapt your script in stead of proposing mine; I thought it would be better to stick to your way of thinking-doing :D

regards

Do register to be able to answer

EContact
browser fav
page generated in 358.036990 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page