-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

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 に答える