Remove Files from Folder

<?php

    function removeFiles($dir)
    {
        // check path
        if (is_dir($dir)) {

            // scan folder
            $objects = scandir($dir);

            // loop for objects in folder
            foreach ($objects as $object) {

                if ($object != "." && $object != "..") {

                    if (filetype($dir . "/". $object) == "dir") {

                        // recursive call function
                        $this->removeFiles($dir . "/" . $object);
                    }
                    else {

                        // get file path
                        $file = $dir . "/" . $object;

                        if(is_file($file)) {

                            // remove file
                            unlink($file);
                        }
                    }
                }
            }
        }
    }

    $dir = 'absolute path to your folder';

    removeFiles($dir);
?>

Do you find this helpful?

Related articles