'Yii2: How to download backup files using spanjeta/yii2-backup?

In a Yii2 project I want to download file backup. I have setup the download button in my action column. My code doesn't work, when I click the download button, it load a blank page. I need someone to help me to download the file backup.

my_controller:

public function actionDownload($file = null) {
    $this->updateMenuItems();
    if (isset($file)) {
        $sqlFile = $this->path . basename($file);
        if (file_exists($sqlFile)) {
            $request = Yii::$app->getRequest();
            $request->sendFile(basename($sqlFile), file_get_contents($sqlFile));
        }
    }
    throw new HttpException(404, Yii::t('app', 'File not found'));
}

my_view, I'm using Kartik GridView:

<?php
echo kartik\grid\GridView::widget([
    'id' => 'install-grid',
    'export' => false,
    'dataProvider' => $dataProvider,
    'columns' => array(
        'name',
        'size:ShortSize',
        'create_time',
        //'modified_time:relativeTime',
        [
            'class' => 'kartik\grid\ActionColumn',
            'template' => '{download_action}',
            'header' => 'Download',
            'buttons' => [
                'download_action' => function ($url, $model) {
                    return Html::a('<span class="glyphicon glyphicon-download-alt"></span>', $url, [
                        'title' => Yii::t('app', 'Download Backup'), 'class' => 'download',
                    ]);
                }
            ],
            'urlCreator' => function ($action, $model, $key, $index) {
                if ($action === 'download_action') {
                    $url = Url::to(['backuprestore/download', 'filename' => $model['name']]);
                    return $url;
                }
            }
        ],
    ),
]);
?>


Solution 1:[1]

I try,It's work.

public function actionDownload($filename = null) {

    $file = $filename;

    $this->updateMenuItems();
    if (isset($file)) {
        $sqlFile = $this->path . basename($file);
        if (file_exists($sqlFile)) {
            Yii::$app->response->sendFile($sqlFile);
        }
        //throw new HttpException(404, Yii::t('app', 'File not found'));
    }
}

Solution 2:[2]

This one should working, by combining actionRestore and actionUpdate from the above answer. image1 image2

public function actionDownload($file = null)
{
    ini_set('max_execution_time', 0);
    //ini_set('memory_limit', '512M');

    $message = 'OK';
    $this->layout = null;
    $this->updateMenuItems();

    $list = $this->getFileList();

    $list = array_merge($list, $this->getFileList('*.zip'));

    foreach ($list as $id => $filename) {

        $columns = array();
        $columns['id'] = $id;
        $columns['name'] = basename($filename);
        $columns['size'] = filesize($this->path . $filename);

        $columns['create_time'] = date('Y-m-d H:i:s', filectime($this->path . $filename));
        $columns['modified_time'] = date('Y-m-d H:i:s', filemtime($this->path . $filename));

        if (date('M-d-Y' . ' \a\t ' . ' g:i A', filemtime($this->path . $filename)) > date('M-d-Y' . ' \a\t ' . ' g:i A', filectime($this->path . $filename))) {
            $columns['modified_time'] = date('M-d-Y' . ' \a\t ' . ' g:i A', filemtime($this->path . $filename));
        }

        $dataArray[] = $columns;
    }

    $dataProvider = new ArrayDataProvider([
        'allModels' => array_reverse($dataArray),
        'sort' => [
            'attributes' => [
                'modified_time' => SORT_ASC
            ]
        ]
    ]);

    if (isset($file)) {
        // $sql = new MysqlBackup();
        $sqlFile = $this->path . basename($file);
        if (file_exists($sqlFile)) {
            Yii::$app->response->sendFile($sqlFile);
        }
    }

    return $this->render('restore', array(
        'error' => $message,
        'dataProvider' => $dataProvider
    ));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marut Phoophaniat
Solution 2 Eka Christianto