PHP删除完整文件夹

时间:2012-04-11 22:31:10

标签: php linux delete-file

我有以下功能,我试图删除一个完整的文件夹,但它似乎没有删除任何想法或建议?

public function submit()
{
        $location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

        $folderName = $this->quote->getCompanyDetails()->companyName;

        $data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;

        $this->load->view('submit',$data);

        $this->quote->removeQuote();

        if(is_dir($location.$folderName) === TRUE)
        {
            $files = array_diff(scandir($location.$folderName), array('.','..'));

            foreach($files as $file)
            {
                Delete(realpath($location.$folderName).'/'. $file);
            }
            return rmdir($location.$folderName);
        }
        else if(is_file($location.$folderName) === TRUE)
        {
            return unlink($location.$folderName);
        }
        return FALSE;
}

更新

public function submit()
{
        $location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

        $folderName = $this->quote->getCompanyDetails()->companyName;

        $data['companyContact'] = $this->quote->getCompanyDetails()->companyContact;

        $this->load->view('submit',$data);

        //$this->quote->removeQuote();

        $this->removeFolder();
}

private function removeFolder(){
        $location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

        $folderName = $this->quote->getCompanyDetails()->companyName;

        foreach(glob($location.$folderName.'/*') as $file)
        {
            if(is_dir($location.$folderName))
            {
                rmdir($location.$folderName);
            }else{
                unlink($location.$folderName);
            }
            rmdir($location.$folderName);
        }
}

1 个答案:

答案 0 :(得分:1)

您无法在一次通话中删除完整文件夹。你应该递归地做到这一点:

function rrmdir($dir) {
    foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            rrmdir($file);
        else
            unlink($file);
    }
    rmdir($dir);
}