Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getting mixed up with folders

Posted on 2004-08-17
2
Medium Priority
?
186 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
[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
2 Comments
 
LVL 1

Accepted Solution

by:
a2liter earned 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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 …
Suggested Courses

636 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