• View our reviews on Hot Scripts
  • JS Tutorial

Recent Comments

Powered by Disqus

Back to articles

All files in directory

Sometimes there is a need to process all files in one directory, for example resize all images or collect all CSS files, minify them and include in on file, and many other practises. Here is a pretty simple way to do it in PHP:

//provide path of directory
//in this example we'll use this same directory
$d = new DirectoryIterator("./");

foreach($d as $f)
    //$f holds file object
    //most common thing we want to do with it is to get file name
    $name = $f->getFilename();
    //and now we can do what we want with it
    //for example check for specific file name
    if(strtolower($name) == "style.css")
        //we have our file
    //or check extension
    $parts = explode(".", $name);
    if(strtolower($parts[sizeof($parts)-1]) == "css")
        //this is a css file
        //lets include it
        //or get contents
        $contents = file_get_contents("./".$name);

You may also be interested in:

Powered by

blog comments powered by Disqus