faressoft
faressoft

Reputation: 19651

How to print all files' names in the "file" folder using php

How to print all files' names in the "file" folder using php ?

/file/

Upvotes: 0

Views: 3185

Answers (4)

codaddict
codaddict

Reputation: 455400

foreach(glob('path/to/directory/file/*') as $file)
        echo $file,"\n";
}

or just

print_r(glob('path/to/directory/file/*'));

Upvotes: 2

yossi
yossi

Reputation: 13325

you can use glob

$folder = "file";
$mask = "*.*";
$files = glob("" . $folder . $mask);
foreach ($files as $file)
{
   $file_name = basename($file,substr($mask,1));  // cut the folder and extension
   echo  $file_name;
}

Upvotes: 1

nan
nan

Reputation: 20316

From: PHP: List Contents of a Directory
Place your directory path instead of ".". Note that it does not display hidden files (starting with . in Linux)

 // open this directory 
$myDirectory = opendir(".");

// get each entry
while($entryName = readdir($myDirectory)) {
 $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

// count elements in array
$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
  print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
  print("<td>");
  print(filetype($dirArray[$index]));
  print("</td>");
  print("<td>");
  print(filesize($dirArray[$index]));
  print("</td>");
  print("</TR>\n");
 }
}
print("</TABLE>\n");

Upvotes: 2

ajreal
ajreal

Reputation: 47331

DirectoryIterator

$dir = new DirectoryIterator('/file/');   <-- remember to put in absolute path
foreach ($dir as $fileinfo) 
{
  if (!$fileinfo->isDot() && $fileinfo->getType()!='dir') 
  {
    var_dump($fileinfo->getFilename());
  }
}

Upvotes: 3

Related Questions