0

lets say I have a folder on a webhost that is called sebis_files and this folder contains some files, maybe pictures, docs...

I want to return the contents of this folder on a separate page, something like:

$row = get dir host/sebis_files*//everything

for ( $row !== 0){ //for every valid file
   echo $row . "<br/>"; //return name of file
}
SebastianZdroana
  • 209
  • 1
  • 11
  • see http://stackoverflow.com/questions/6155533/loop-code-for-each-file-in-a-directory – Ismail Moghul Nov 29 '15 at 14:24
  • 5
    Possible duplicate of [Get the Files inside a directory](http://stackoverflow.com/questions/1086105/get-the-files-inside-a-directory) – Kenney Nov 29 '15 at 14:25

3 Answers3

1

You can use opendir and readdir. Here's a breakdown:

We use __DIR__ to make the path relative to the directory of the current script, just to be safe:

$dir = __DIR__ . '/sebis_files'; 

Next we open the directory to read it's entries. We call readdir, which will return a 'resource' object, or false if $dir is not a readable directory:

if ($dh = opendir($dir))
{

The directory is successfully opened. We now call readdir on that directory. We use the return value of opendir, the mysterious 'resource' object, that will let PHP know what directory we are reading.

Every time we call readdir it will give us the next entry in the directory. When there are no more entries, readdir will return false:

      while ( ($entry = readdir($dh)) !== false)
      {          

We have read a directory $entry: the name of a file or sub-directory inside $dir. So, it's not a full pathname. Let's print it's name, along with whether it is a directory or a file. We will use is_file and is_dir, but we will need to pass the full pathname (hence "$dir/$entry"):

          if ( is_dir( "$dir/$entry" ) )
              echo "Directory: $entry<br/>";
          else if ( is_file( "$dir/entry" ) )
              echo "File: $entry<br/>";
      }

we are done with the directory, let's close it to free the resource:

      closedir($dh);
 }

But what if $dir cannot be opened for reading? Let's print a warning:

 else
     echo "<div class='warning'>cannot open directory!</div>";
Kenney
  • 9,003
  • 15
  • 21
  • what will the $dh and $dir be defined as? $dh = opendir($dir) and $dir = "mydir"; ? – SebastianZdroana Nov 29 '15 at 14:31
  • The `$dh` from `opendir` is a "directory handle" that you need to pass to `readdir` to say which open directory you are reading. It is similar to `$f = fopen(...)` and `fread( $f, ...)` where you open a file and read it's contents. Don't worry about `$dh`, that is set up properly already. And `$dir` is simply the directory you are wanting to read - you weren't clear on what it is, but you could start with `$dir = "sebis_files"`. – Kenney Nov 29 '15 at 14:35
  • also i keep getting this error for filetype-- Warning: filetype(): Lstat failed for ... in E:\XAMPP\htdocs\host\return.php on line 44 -- line 44: echo "filename: $file : filetype: " . filetype($dir . $file) . "
    ";
    – SebastianZdroana Nov 29 '15 at 14:52
  • Ah yes, I copied that from the php page. You can safely remove that. It's probably because `$dir` doesn't end with a `/` so it creates a wrong filename (like `sebis_filesfile.ext` instead of `sebis_files/file.ext`). I removed it from the answer. – Kenney Nov 29 '15 at 14:55
  • @Kenny also checking the returns of the variables why does opendir return Resource id #3 – SebastianZdroana Nov 29 '15 at 15:43
  • Click on the `opendir` link in the answer and read what it says under "Return Values". – Kenney Nov 29 '15 at 15:45
  • @user3582842 I saw your edit proposal. I updated the answer to explain every step. I hope it is more clear now! – Kenney Nov 29 '15 at 20:18
1

you need is to see this

    <?php
$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>
Waqar Haider
  • 929
  • 10
  • 33
0

You can do it using the glob function :

$dir = "/your/dir/";

if(file_exists($dir))
{
    foreach (glob("$dir*") as $file) 
    {
        if(is_file($file))
        {
            echo basename($file) . "<br />";
        }
    }
}
mazert
  • 50
  • 5