yii\helpers\BaseFileHelper::removeDirectory PHP Method

removeDirectory() public static method

Removes a directory (and all its content) recursively.
public static removeDirectory ( string $dir, array $options = [] )
$dir string the directory to be deleted recursively.
$options array options for directory remove. Valid options are: - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. Defaults to `false`, meaning the content of the symlinked directory would not be deleted. Only symlink would be removed in that default case.
    public static function removeDirectory($dir, $options = [])
    {
        if (!is_dir($dir)) {
            return;
        }
        if (isset($options['traverseSymlinks']) && $options['traverseSymlinks'] || !is_link($dir)) {
            if (!($handle = opendir($dir))) {
                return;
            }
            while (($file = readdir($handle)) !== false) {
                if ($file === '.' || $file === '..') {
                    continue;
                }
                $path = $dir . DIRECTORY_SEPARATOR . $file;
                if (is_dir($path)) {
                    static::removeDirectory($path, $options);
                } else {
                    try {
                        unlink($path);
                    } catch (ErrorException $e) {
                        if (DIRECTORY_SEPARATOR === '\\') {
                            // last resort measure for Windows
                            $lines = [];
                            exec("DEL /F/Q \"{$path}\"", $lines, $deleteError);
                        } else {
                            throw $e;
                        }
                    }
                }
            }
            closedir($handle);
        }
        if (is_link($dir)) {
            unlink($dir);
        } else {
            rmdir($dir);
        }
    }

Usage Example

Esempio n. 1
1
 public function attachFile()
 {
     try {
         $model = $this->owner;
         $fileName = Inflector::slug($model->fullAddress) . '.pdf';
         $folder = $this->getModelSubDir() . '/';
         $model->pdf_path = $fileName;
         $pdf = new Pdf(['mode' => Pdf::MODE_UTF8, 'destination' => Pdf::DEST_FILE, 'content' => Yii::$app->view->render('@frontend/views/site/real-estate/print', ['model' => $model]), 'methods' => ['SetHeader' => ['Rapport ' . $model->fullAddress], 'SetFooter' => ['|Pagina {PAGENO}|']], 'filename' => Yii::getAlias('@uploadsBasePath') . "/files/{$folder}{$fileName}"]);
         $this->file = $pdf;
         BaseFileHelper::removeDirectory(Yii::getAlias('@uploadsBasePath') . "/files/{$folder}");
         if (!BaseFileHelper::createDirectory(Yii::getAlias('@uploadsBasePath') . "/files/{$folder}")) {
             throw new Exception(Yii::t('app', 'Failed to create the file upload directory'));
         }
         // Save and update model
         $pdf->render();
         $model->save();
         return true;
     } catch (yii\base\Exception $e) {
         return $e->getMessage();
     }
 }
All Usage Examples Of yii\helpers\BaseFileHelper::removeDirectory