Yii2:如何使用 spanjeta/yii2-backup 下载备份文件?
Yii2: How to download backup files using spanjeta/yii2-backup?
在一个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
,我正在使用 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;
}
}
],
),
]);
?>
我试过了,成功了。
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'));
}
}
通过结合上述答案中的 actionRestore 和 actionUpdate,这个应该可以工作。
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
));
}
在一个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
,我正在使用 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;
}
}
],
),
]);
?>
我试过了,成功了。
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'));
}
}
通过结合上述答案中的 actionRestore 和 actionUpdate,这个应该可以工作。 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
));
}