Manolo
Manolo

Reputation: 26460

Get files names inside a directory path

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

Answers (3)

codelover
codelover

Reputation: 317

USE

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

CHECK HERE

Upvotes: 0

Okw
Okw

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

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

Related Questions