Solved

getting mixed up with folders

Posted on 2004-08-17
2
180 Views
Last Modified: 2006-11-17
Hi,

I've got a directory

/scams/examples

which contains atm 5 subdirectories. each of the these subdirectories contains some images.
i'm trying to make an <ul> in the form
<ul>
<li>foldername</li>
          <ul>
           <li>image url</lil>
           <li>etc</li>
          </ul>
<li> etc </li>
</ul>

i wrote some php which vaguely worked but only looped once. have been moving bits around but now it works even less. i think i've gotten a bit mixed up with so many sub loops. can someone have a look please.

<ul>
<?
$path = "scams/examples";
$dir_handle = @opendir($path) or die("Unable to open $path");
chdir($path);
while (false !== ($file = readdir($dir_handle))) {
       //check for scam folder names
       if (is_dir($file) && $file != "." && $file != "..") {
           //echo ("hello?<br>");
           echo ("<li>$file</li>\n");
           $path2 = ("$file");
           $dir_handle2 = @opendir($path2) or die("Unable to open $path2");
           chdir($path2);
           while (false !== ($file2 = readdir($dir_handle2))) {
                  echo ("  <ul>\n");
                  //check for scam images in folder $file
                  if (!is_dir($file2) && $file2 != "." && $file2 != "..") {
                          $query="SELECT id,subject FROM examples WHERE filename='$file2'";
                            $result=@mysql_query($query);
                          if ($result){
                                                   while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
                                        //echo ("$file2");
                                        $page="scams/phishing.php?id=$row[0]";
                                        echo ("&nbsp;&nbsp;<li><a href=\"$root$page\">$row[1]</a></li>\n");
                                        mysql_free_result($result);
                                     }
                           }
                    }
           closedir($dir_handle2);
           echo ("</ul>\n");
       }

}
       chdir($path);


}
closedir($dir_handle);

?>

</ul>


the source comes out as

<ul>
<li>HSBC</li>
  <ul>
</ul>

</ul>

at one point i had it making the creating the a href for the hsbc image but now it doesnt. (there's was only one image in the hsbc folder.

thanks for any assistance.

0
Comment
Question by:browolf
2 Comments
 
LVL 1

Accepted Solution

by:
a2liter earned 125 total points
ID: 11844175
Here ya go. This will give you the basic structure of what you are after. You will have to do some error checking to see if arrays returned from the function contain files, if a dir has no file you will get an error. I have only tested this function with 1 level deep directories. you should be able to modify it to do recursive.

<?php

$base="D:\My Documents\My Pictures";

function listit($path){
 if($handle=opendir($path)){
  while (false !== ($file = readdir($handle))) {
   if ($file != "." && $file != "..") {
    if(is_dir("$path/$file")){
     $dirs[] = $file;
    }else{  
     $files[] = $file;
    }
   }
  }
 }
 $data = array($dirs, $files);
 return $data;
}

$data = listit($base);
print "<ul>\n";
foreach($data[0] as $dir){
 print "<li>$dir</li>\n";
 $newpath = "$base\\$dir";
 $data2 = listit($newpath);
 print "<ul>\n";
 foreach($data2[1] as $image){
  print "<li>$image</li>\n";
 }
 print "</ul>\n";
}
print "</ul>\n";
?>
0
 
LVL 3

Author Comment

by:browolf
ID: 11845355
thanks a lot thats ace. I don't think i'd have ever thought of doing it like that. I'm newish to php,
all i had to change is this

$newpath = "$base/$dir";

cos  my site is hosted on a linux/unix based system. I dont need any error checking cos there wont ever be empty folders. i'll only create folders when i have something to put in them. there'll be a slight window i suppose.

the page in question is at:  http://www.antiphishing.org.uk/scams.php


regards

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

786 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