Yii Hint For Files Handling

This post will help you to learn “how to delete files in yii framework”. I deleted singele file and all files from directory.

Delete All Files Inside Directory

php glob() method used to read all files from directory of yii application and unlink() method used to delete the files.

   //your directory                                
   $dir=Yii::app()->basePath.'/files/foldername/';
   foreach(glob($dir.'*.*') as $files){
            unlink($files);
   }

When we use above method , All files will be deleted inside directory

Delete Single File

Get the base directory of yii application using Yii::app()->basePath and assign the folder path, file path to delete the file. apply unlink() to delete the file.

   //your file path
   $filepath=Yii::app()->basePath.'/files/foldername/filename.ext';
   unlink($filepath);