Jump to content


Photo

get Directory/File info


  • Please log in to reply
5 replies to this topic

#1 tensuke

tensuke
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 March 2006 - 11:38 PM

Hey,I'm new to PHP so I'm still learning,but I have a question:
How do you get the number of files in a certain directory and store that into a variable?

And also,what would be the correct way display that variable's data?

Thanks for any help. ^_^

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 22 March 2006 - 01:51 AM

Check the manual:

[a href=\"http://www.php.net/dir\" target=\"_blank\"]http://www.php.net/dir[/a]

There is a lot of good info there. Especially the user comments at the bottom.

There are a couple of functions there that will return an array with all the file names and subdirectory names in an array that you can then do a count on to get the number of files in the directory.

To display the value of nearly any variable, you can use either echo or print.

[a href=\"http://www.php.net/echo\" target=\"_blank\"]http://www.php.net/echo[/a]
[a href=\"http://www.php.net/print\" target=\"_blank\"]http://www.php.net/print[/a]

For arrays, use the print_r function:

[a href=\"http://www.php.net/print_r\" target=\"_blank\"]http://www.php.net/print_r[/a]

#3 tensuke

tensuke
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 March 2006 - 03:59 AM

Thanks for the info,that helped. :D

EDIT:
Howdy,
I'm using
<?php
$dir = "/ipbfree";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
   $ipbfree[] = $filename;
}

echo count($ipbfree);
?>
to scan the files in the directory ipbfree.Ipbfree is in the same folder as the index.html that is running the script.
But everytime I try to fix it,it doesn't display anything.The number is between [ ],and so it just shoes [].
I am using PHP 4.3.11.Any ideas?

#4 tensuke

tensuke
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 22 March 2006 - 11:37 PM

Anyone?

#5 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 23 March 2006 - 01:25 AM

php cannot be run in an html file, rename it to index.php

#6 kenrbnsn

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

Posted 23 March 2006 - 02:22 AM

Look at the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob[/a]() function. It's much easier to use.
<?php
$ipbfree = glob('/ipbfree');
echo count($ipbfree);
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users