Solved

getting mixed up with folders

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now