?
Solved

listing directory and filtering

Posted on 2003-03-26
13
Medium Priority
?
259 Views
Last Modified: 2006-11-17
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<br>";
$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 "<br>  <a href=\"$file\">$file</a>\n";
$counter++;
}
}
closedir($handle);
?>
0
Comment
Question by:fin51
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8212437
<?
echo "Click on customer to view transfer stats<br>";
$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 "<br>  <a href=\"$filename$ext\">$filename</a>\n";
       $counter++;
     } // if ok
}
}
closedir($handle);
?>

0
 

Author Comment

by:fin51
ID: 8212589
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.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8212637
sorry, I miswrote the tests

suppress the NOT (!)

I'm stupid :D

0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 15

Expert Comment

by:VGR
ID: 8212679
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' ;-)
0
 

Author Comment

by:fin51
ID: 8212680
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.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8212684
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><br>";
   } // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
0
 

Author Comment

by:fin51
ID: 8212698
Sorry but what do you mean by supressing the NOT (!)
Just trying to learn and understand. This is new to mean.
0
 

Author Comment

by:fin51
ID: 8212744
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
 

Author Comment

by:fin51
ID: 8212756
Ok the last one works fine but it show files with  -days and -months but everything else is working perfect.
0
 
LVL 15

Accepted Solution

by:
VGR earned 200 total points
ID: 8212820
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><br>";
    } // if file not days nor month(e)s
  } // if file OK
} // if dir opened OK
closedir($dir_handle);
?>
0
 
LVL 15

Expert Comment

by:VGR
ID: 8212830
in the "   if ($ok) {  " just add :

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

and you are done with it.
     
0
 

Author Comment

by:fin51
ID: 8212861
Works great, thank you for your time and I understand
how it all works now.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8212886
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question