Solved

getting mixed up with folders

Posted on 2004-08-17
2
181 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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