Version: current

     1 <?php
     2 /**
     3  * Save actual files size to DB
     4  * {@link File} object
     5  *
     6  * @package sapphire
     7  * @subpackage filesystem
     8  */
     9 class SaveFileSizeTask extends BuildTask {
    10
    11     protected $title = 'Save files size task';
    12
    13     protected $description = 'Save actual files size to database';
    14
    15     /**
    16      * Check that the user has appropriate permissions to execute this task
    17      */
    18     public function init() {
    19         if(!Director::is_cli() && !Director::isDev() && !Permission::check('ADMIN')) {
    20             return Security::permissionFailure();
    21         }
    22
    23         parent::init();
    24     }
    25
    26     /**
    27      * Actually clear out all the images
    28      */
    29     public function run($request) {
    30
    31         $files = DB::Query("SELECT ID, Filename FROM File WHERE ClassName <> 'Folder'");
    32         if ($files->numRecords()) {
    33             foreach($files as $file) {
    34                 $size = 0;
    35                 $filename = $file['Filename'];
    36
    37                 $baseFolder = Director::baseFolder();
    38                 if(strpos($filename, $baseFolder) === false) {
    39                     // otherwise assume silverstripe-basefolder
    40                     $filename = "{$baseFolder}/{$filename}";
    41                 }
    42                 if (file_exists($filename)) {
    43                     $size = filesize($filename);
    44                 }
    45                 echo "{$filename} - {$size} \n";
    46
    47                 DB::Query("UPDATE File SET FileSize = {$size} WHERE ID = " . $file['ID']);
    48             }
    49         }
    50
    51         echo "File size in DB refreshed.";
    52     }
    53
    54 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -