Forum Moderators: coopster

Message Too Old, No Replies

Directory Content

         

neroag

1:08 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Hi Guys
The snippet below prints directories for me but im looking for a way to enter each diretory & print the contents of those directories in the format ..

Directory A
Content 1
Content 2
etc

Directory B
Content 1
Content 2
etc

Im trying to set up where i can add directory names & contents into a MySQL database

<?php
if ($handle = opendir('/var/www/html/project/')) {
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= "..") {
echo "$file\n <br>";
}
}
closedir($handle);
}
?>

Thanx Everyone

jatar_k

4:53 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I found this snippet, it shows filesizes and paths, it works, you can edit it to show exactly what you need

<?php 
function display($dir) {
$html = '';
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(is_dir($f)) {
$ds = $dir.$f."/";
$innerstuff = display($ds);
$html .= $innerstuff;
}else{
$html .= "<tr><td>".$dir.$f."</td><td>".filesize($dir.$f)."</td></tr>\n";
}
}
}
return $html;
}
echo "<table width=80% cols=2>\n";
$stuff = display("./");
echo $stuff;
echo "</table>\n";
?>

neroag

2:01 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



Thank you for that but not quite what im looking for sorry
i need to be able to list the folders in a directory
then enter each folder in that directory & list the files in it in the format ..

folder a
photoa.jpg
photo2.jpg

folder b
myphoto1.jpg
myphoto2.jpg

I have had a go at trying to get the following to work but it will only list the folders in the directory

<?php
if ($handle = opendir('/var/www/html/project/photos')) {
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= "..") {
echo "$file <br>";
}
}
$dir = "$file";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file2 = readdir($dh))!== false) {
echo "filename: $file2 : filetype: " . filetype($dir . $file2) . "\n";
}
closedir($dh);
}
closedir($handle);
}
}
?>

jatar_k

3:21 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it does what you need it is just a matter of playing with the output

what about this

<?php 
function display($dir) {
$html = '';
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(is_dir($f)) {
$ds = $dir.$f."/";
$innerstuff = '<tr><td>&nbsp;</td></tr><tr><td><b>' . $ds . '</b></td></tr>' . display($ds);
$html .= $innerstuff;
}else{
$html .= "<tr><td>".$f."</td></tr>\n";
}
}
}
return $html;
}
echo "<table width=80% cols=2>\n";
$stuff = display("./");
echo $stuff;
echo "</table>\n";
?>

neroag

3:49 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



Sorry im a noob at php but i am trying to have a go with it lol,
so which variable holds the directory name
& which variable holds the contents of the directories?
please