[英]Download a file from command line in laravel artisan
我正在使用laravel Artisan Commands从公共文件夹下载文件。
其实我想下载一个我在公共文件夹中成功制作的zip文件。
当我按下按钮点击方法时,我能够下载文件。
但是当我在控制台中执行命令时,它不起作用,也不会抛出任何错误。
public function downloadExportRawData() {
$a=public_path().'/temp/rawexport/rawexportproduct.zip';
$path='/temp/rawexport/rawexportproduct.zip';
if(file_exists('temp/rawexport/rawexportproduct.zip')){
return Response::download($a);
}else{
return "hello";
}
}
试过这个......
public static function downloadZip($rand_folder, $zipname, $fileName) {
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="' . $fileName . '.zip"');
header('Content-Length: ' . filesize($zipname));
$handle = fopen($zipname, 'rb');
//exec("rm -r ".$temp);
while (!feof($handle)) {
echo fread($handle, 1048576);
ob_flush();
flush();
}
fclose($handle);
// exec("rm -r " . $rand_folder);
}
而命令是
php artisan product:export --from=http://localhost/valuable-app/dev-master/rest_app/public/ --productid=5b273b0e4bb5995b108b4576 --tokenexport=354c3ddb2bd45aa6e5c4f749749b0b58cbacef48
主要问题是它是通过控制器中的功能而不是下载它。 我试图回应它以zip格式打印代码的响应意味着一个编码的表单,其中包含与我的zip文件相关的东西意味着它打印zip而不是下载它。 我第一次使用Artisan。 知道如何使用用户定义的Artisan命令下载该文件。
我在过去两周尝试这个但是无法做任何事情。
这个怎么样:
首先确定要下载的文件然后调用
string shell_exec ( string $cmd )
你可以用wget来获取文件。
例如,在您的命令中
$fileName = $this->argument('filename');
$file = url('storage/public/' . $fileName);
shell_exec("wget $file");
$this->info('downloading');
如果你使用控制台,http下载将无法正常工作(你没有使用浏览器)。
相反,您可以将文件移动到您希望的目的地或类似的东西,但不能通过控制台下载
控制台不会处理http响应下载(不会保存)我认为你应该考虑在控制台中添加第二个参数:-- --destination=/home/user/downloads/filetosaveas.extension
并在你的代码中使用curl下载
或者更简单的使用核心卷曲:
curl http://example.com --output my.file --silent
如果你真的在Laravel Docs网站上完成了很好的写作指令指南,那就太好了 。
简而言之,主要有两种注册工匠命令的方法。 是的,一个是另一个的替代品。
在我的routes/console.php
,我指定了如下内容:
Artisan::command('download:file', function () {
// your code goes here.
})->describe('Download a file.');
现在为新创建的命令执行帮助例程。
-> php artisan help download:file
Description:
Donwload a file.
Usage:
donwload:file
//...
使用php artisan make:command EmailFileCommand
生成命令类php artisan make:command EmailFileCommand
。 此命令将创建app/Console/Commands/EmailFileCommand.php
。 更新文件以指定我们希望它执行的操作。
// ...
class EmailFileCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'email:file';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Email a file.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// your code goes here.
}
}
假设您想通过http请求调用命令。 在我的routes/web.php
文件中,
//...
use Artisan;
//...
Route::get('/download', function () {
Artisan::call('download:file');
});
Route::get('/email', function () {
Artisan::call('email:file');
});
//...
现在执行您的http请求/download
和/email
。
要下载文件,您可以使用Laravel的文件系统 。
//...
use Storage;
//...
Storage::download('file.jpg')
这个帖子已经从我的博客文章被提取这里 。
可以使用shell_exec()命令通过PHP函数或脚本运行终端命令。 它将在不打开终端窗口的情况下执行命令。 命令的行为与原始命令略有不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.