admin
  • 0
Begginer

Deleting all files from a folder using PHP?

  • 0

For example I had a folder called `Temp’ and I wanted to delete or flush all files from this folder using PHP. Could I do this?

  1. If you want to delete everything from folder (including subfolders) use this combination of array_map, unlink and glob:

    array_map( ‘unlink’, array_filter((array) glob(“path/to/temp/*”) ) );

    • 0
  2. This code from http://php.net/unlink:

    /**
     * Delete a file or recursively delete a directory
     *
     * @param string $str Path to file or directory
     */
    function recursiveDelete($str) {
        if (is_file($str)) {
            return @unlink($str);
        }
        elseif (is_dir($str)) {
            $scan = glob(rtrim($str,'/').'/*');
            foreach($scan as $index=>$path) {
                recursiveDelete($path);
            }
            return @rmdir($str);
        }
    }
    • 0
Leave an answer

Leave an answer

Browse