• 0

[PHP] Check to see if folder contains files


Question

I know things like this work...

$filename = "../test2/file.txt";

if (file_exists($filename)) {
	echo "file is there!";
}

But when I try putting /test2/* into $filename, it doesn't say a file exists.

Anybody know how I can check to see if ANY files exist in a certain directory?

Thanks!

3 answers to this question

Recommended Posts

  • 0

Something like this?

<?php
 $folder = "../test";

 if ($handle = opendir('$folder'))
 {
  $filecount = 0;
  # go through directory...
   while (false !== ($file = readdir($handle)))
  {
	   #echo "Processing file: ".$file;
	   $filecount++; # inc. file count.
  }

  if($filecount > 0)
 {
   echo "There were exactly ".$filecount ." file(s) in folder " . $folder;
 }
 else
 {
  echo "No files in ". $folder . "!";
 }
?>

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.