SORU
28 Temmuz 2010, ÇARŞAMBA


Eğer dosya dizini silin?

Ne içindeki tüm dosyaları ile bir dizin silmek için en kolay yolu acaba?

rmdir(PATH . '/' . $value); bir klasörü silmek için kullanıyorum, ancak, eğer içinde dosya varsa, sadece silebilirsiniz.

CEVAP
28 Temmuz 2010, ÇARŞAMBA


En az iki seçenek vardır nowdays vardır.

  1. Klasörü silmeden önce, tüm dosya ve klasörleri (ve bu sayede özyineleme!) silin. İşte bir örnek:

    public static function deleteDir($dirPath) {
        if (! is_dir($dirPath)) {
            throw new InvalidArgumentException("$dirPath must be a directory");
        }
        if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
            $dirPath .= '/';
        }
        $files = glob($dirPath . '*', GLOB_MARK);
        foreach ($files as $file) {
            if (is_dir($file)) {
                self::deleteDir($file);
            } else {
                unlink($file);
            }
        }
        rmdir($dirPath);
    }
    
  2. Ve eğer 5.2 kullanıyorsanız özyineleme Kendin yapmaya gerek kalmadan bunu yapmak için bir Recursiveİterator kullanabilirsiniz:

    $dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
    $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($it,
                 RecursiveIteratorIterator::CHILD_FIRST);
    foreach($files as $file) {
        if ($file->isDir()){
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($dir);
    

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • bmarian22

    bmarian22

    22 Aralık 2007
  • jesiel santos

    jesiel santo

    15 Ocak 2009
  • Keith Anthe

    Keith Anthe

    26 NİSAN 2011