Jump to content


Photo

Scanning a Directory


  • Please log in to reply
5 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 March 2006 - 08:29 PM

How can you scan a directory and list all the files in it? I know its a pretty simple script, but I lost the tuts to it. Links/tuts/examples would be greatly appreciated.

Thanks in advanced.

edit: I have

<?php
$dir    = '/upload';
$files1 = scandir($dir);

print_r($files1);
?> 



but it says "Fatal error: Call to undefined function: scandir() in /home/lpxxfain/public_html/scandir.php on line 11"

Any ideas?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 March 2006 - 08:49 PM

Look at the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob[/a]() function.

Ken

#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 March 2006 - 08:54 PM

Or

<?php
foreach (glob("*.txt") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
}
?>

When I run it, it comes out like

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
0.gif size 221 1.gif size 203 2.gif size 225 3.gif size 225 4.gif size 229 403.shtml size 1490 404.shtml size 1475 5.gif size 225 500.shtml size 1455 6.gif size 226 7.gif size 215 8.gif size 224 9.gif size 230 BagFullOfSh1t.zip size 22519 BagFullOfSh1t1.0.txt size 5 BagFullOfSh1t1.0.txt.zip size 34653 BagFullOfSh1t1.1.txt size 5 BagFullOfSh1t1.1.zip size 33512 BagFullOfSh1t11.txt size 33497 Fountains Of Wayne - Stacys Mom.mp3 size 4757017 Linkin Park - Hit The Floor.mp3 size 3940891 aboutus.php size 16665 animated.gif size 328841 clip.mp3 size 1342400 contact.php size 16665 db_mysql.inc size 10345 getfile.php size 321 index.php size 1141 intro.swf size 72788 labels.rdf size 2216 labels_1.rdf size 2216 members.php size 16665 noel.jpg size 5575 noel2.jpg size 6112 novell_login.zip size 15843 rules.php size 16665 scandir.php size 433 scandir.txt size 426 test.php size 1055 tournaments.php size 16665 update.zip size 33512 upload.php size 11348 xmas.mp3 size 4614815 zero.zip size 61470
[/quote]

Is there any way so that </br> can be included with every different file?



edit: fixed. Thanks for your quick help! <3

#4 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 March 2006 - 09:25 PM

Oops, 1 more question. Is there any way to list how many files there are? Like, "There are 426 Files in this directory"

#5 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 08 March 2006 - 10:27 PM

^ is that even possible?

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 March 2006 - 04:44 AM

Yes, it's possible:
<?php
$dir_tmp = glob('*.txt');
foreach ($dir_tmp as $filename) {
   echo $filename . ' size ' . filesize($filename) . "<br />\n";
}
echo 'There are ' . count($dir_tmp) . ' files in this directory<br>';
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users