-2

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

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);
        }
}
4

1 回答 1

1

您不能在一次通话中删除整个文件夹。你应该递归地这样做:

function rrmdir($dir) {
    foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            rrmdir($file);
        else
            unlink($file);
    }
    rmdir($dir);
}
于 2012-04-11T22:40:27.457 回答