Reputation: 26460
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.
Upvotes: 2
Views: 524
Reputation: 63
<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>
Upvotes: 1
Reputation: 68556
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>";
}
Upvotes: 2