45

I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?

Arun Annamalai
  • 785
  • 1
  • 7
  • 20

9 Answers9

121

The simplest and most fun way (imo) is glob

foreach (glob("*.*") as $filename) {
    echo $filename."<br />";
}

But the standard way is to use the directory functions.

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: .".$file."<br />";
        }
        closedir($dh);
    }
}

There are also the SPL DirectoryIterator methods. If you are interested

xkeshav
  • 53,360
  • 44
  • 177
  • 245
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
18

This is what I like to do:

$files = array_values(array_filter(scandir($path), function($file) use ($path) { 
    return !is_dir($path . '/' . $file);
}));

foreach($files as $file){
    echo $file;
}
Sebastian Viereck
  • 5,455
  • 53
  • 53
Bruno Calza
  • 2,732
  • 2
  • 23
  • 25
16

There is this function scandir():

$dir = 'dir';
$files = scandir($dir, 0);
for($i = 2; $i < count($files); $i++)
    print $files[$i]."<br>";

More here in the php.net manual

bar
  • 161
  • 1
  • 2
3

If you have problems with accessing to the path, maybe you need to put this:

$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/cv/"; 

// Open the folder
 $dir_handle = @opendir($root . $path) or die("Unable to open $path");
MontDeska
  • 1,617
  • 19
  • 16
3

There is a glob. In this webpage there are good article how to list files in very simple way:

How to read a list of files from a folder using PHP

Mathias
  • 41
  • 1
  • 8
    Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Aug 04 '13 at 15:52
3

You can use standard directory functions

$dir = opendir('/tmp');
while ($file = readdir($dir)) {
    if ($file == '.' || $file == '..') {
        continue;
    }

    echo $file;
}
closedir($dir);
Vlad Miller
  • 2,255
  • 1
  • 20
  • 38
2

Check in many folders :

Folder_1 and folder_2 are name of folders, from which we have to select files.

$format is required format.

<?php
$arr = array("folder_1","folder_2");
$format  = ".csv";

for($x=0;$x<count($arr);$x++){
    $mm = $arr[$x];

    foreach (glob("$mm/*$format") as $filename) {
        echo "$filename size " . filesize($filename) . "<br>";
    }
}
?>
Rishabh
  • 546
  • 5
  • 8
0

There is also a really simple way to do this with the help of the RecursiveTreeIterator class, answered here: https://stackoverflow.com/a/37548504/2032235

Community
  • 1
  • 1
jim_kastrin
  • 4,830
  • 2
  • 26
  • 28
0
<html>
<head>
<title>Names</title>
</head>
<body style="background-color:powderblue;">

<form method='post' action='alex.php'>
 <input type='text' name='name'>
<input type='submit' value='name'>
</form>
Enter Name:
<?php

  if($_POST)
  {
  $Name = $_POST['name'];
  $count = 0;
  $fh=fopen("alex.txt",'a+') or die("failed to create");
  while(!feof($fh))
  {
    $line = chop(fgets($fh));
    if($line==$Name && $line!="")
    $count=1;
  }
  if($count==0 && $Name!="")
  {
    fwrite($fh, "\r\n$Name"); 
 }
  else if($count!=0 && $line!="") 
  { 
    echo '<font color="red">'.$Name.', the name you entered is already in the list.</font><br><br>';
  }
  $count=0;
  fseek($fh, 0);
  while(!feof($fh))
  {
    $a = chop(fgets($fh));
    echo $a.'<br>';
    $count++;
  }
  if($count<=1)
  echo '<br>There are no names in the list<br>';
  fclose($fh);
  }
?>
</body>
</html>
  • 1
    While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – radoh Dec 03 '16 at 20:51