2

Is there any PHP function to retrieve the file/s name/s inside a directory path?

E.g., I have a CSS file inside /css, and I want to get this file's name.

Solution:

As @ShankarDamodaran suggested, I used:

//Get CSS file/s name/s
chdir($_SERVER['DOCUMENT_ROOT'] . '/css'); //<--- Set the directory here...
foreach (glob("*.css") as $filename) {     //<----Get only CSS files
    $CSSfiles[] = $filename;
}

This will return an array ($CSSfiles) with the names of the CSS files.

Manolo
  • 24,020
  • 20
  • 85
  • 130

3 Answers3

2

Make use of glob() for this

<?php
chdir('../css'); //<--- Set the directory here...
foreach (glob("*.*") as $filename) { //<--- Pass *.css , (If you need just the CSS files)
    echo $filename."<br>";
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1
<pre>
<?php
if ($handle = opendir('css')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }
}
closedir($handle);
?>
</pre>
Okw
  • 63
  • 6
0

USE

<?PHP echo realpath('YOURFILE.PHP');?>

CHECK HERE

codelover
  • 317
  • 1
  • 11
  • Is this retrieving the file name or the directory name? – Manolo Mar 16 '14 at 09:36
  • @ManoloSalsas ,I HAVE EDITED MY ANSWER..YOU CAN JUST CHECK – codelover Mar 16 '14 at 09:48
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – nKn Mar 16 '14 at 09:51
  • @nKn ,yOU ARE ABSOLUTELY RIGHT..SINCE `REALPATH` WAS A KEYWORD I JUST GAVE THE FULL ANSWER..EITHERWAY I ALSO GAVE A LINK FOR REFERENCE – codelover Mar 16 '14 at 09:54