X10nD
X10nD

Reputation: 22030

Read one file name "Only" and skip to next directory - PHP

I have the following code which reads all filnames from each directory, but I want it to read one filename "only" and skip to the next directory.

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>

How can I read only one filename, then skip to the next directory to read "only" first filename and so on. Please let me know if you require any more information.

Upvotes: 0

Views: 199

Answers (2)

Santa&#39;s helper
Santa&#39;s helper

Reputation: 996

if it doesn't matter which file you read then just put a break; in your while loop or don't even go in a loop just take the $file = readdir($dh); PS: In linux OS be aware of . and .. Also look up scabdir() function

Upvotes: 2

Anand Solanki
Anand Solanki

Reputation: 3425

You need to maintain counter here to do the same.

Do like this:

$dir = "/images/";

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

Let me know for more help!

Upvotes: 1

Related Questions