Saturday, February 26, 2011

Delete all files of directory using Php

<?php
$dir= "/opt/liferay/6.0.4/test2";
   
class Deletefile{
function deleteDirectory($dir) {
    if (!file_exists($dir)) return true;
    if (!is_dir($dir) || is_link($dir)) {
         chmod($dir, 0777);
        return unlink($dir);}
        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') continue;
            if (!self::deleteDirectory($dir . "/" . $item)) {
                chmod($dir . "/" . $item, 0777);
                if (!self::deleteDirectory($dir . "/" . $item)) return false;
            };
        }
        return rmdir($dir);
    }

}

$df = new Deletefile();
$df->deleteDirectory($dir);
echo "Reached here";

No comments:

Post a Comment