2

How can I delete the folder and contents in Yii2?

I've tested it, but it does not work!

rmdir('folder path');
Farshid
  • 497
  • 1
  • 6
  • 20

3 Answers3

5

Use Yii 2.0 built in FileHelper - http://www.yiiframework.com/doc-2.0/yii-helpers-basefilehelper.html#removeDirectory()-detail

FileHelper::removeDirectory('/path/to/dir');
Sergei Beregov
  • 738
  • 7
  • 19
0

The directory is probably not empty. Take a look here to help remove non-empty directories: How do I remove a directory that is not empty?

It sounds like you have PHP debugging turned off, you may want to enter this at the top of any php file that's giving you issues during development to help understand what's up:

error_reporting(-1);
ini_set('display_errors', 'On');

Just be sure to remove it before going live!

Community
  • 1
  • 1
0

Try this

class SiteController extends Controller
{

   public function actionIndex($dir) {

    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }

    }

    return rmdir($dir);
   }
}
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130