代码之家  ›  专栏  ›  技术社区  ›  Marut Phoophaniat

Yii2:如何使用spanjeta/Yii2备份下载备份文件?

  •  0
  • Marut Phoophaniat  · 技术社区  · 8 年前

    在一个Yii2项目中,我想下载文件备份。我已经在我的操作栏中设置了下载按钮。我的代码不工作,当我点击下载按钮时,它加载了一个空白页面。我需要有人帮我下载文件备份。

    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

    <?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;
                    }
                }
            ],
        ),
    ]);
    ?>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Marut Phoophaniat    8 年前

    我努力了,这是工作。

    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'));
        }
    }
    
        2
  •  0
  •   Eka Christianto    4 年前

    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
        ));
    }
    
    推荐文章