7
votes

I am using laravel Artisan Commands to download a file from public folder .

Actually i want to download a zip file which i have successfully made in public folder.

I am able to download the file when I call the methods on button click.

But when I execute the command in console it does not work and also not throw any error.

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";
    }
}

Tried this also...

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);
}

and the command is

php artisan product:export --from=http://localhost/valuable-app/dev-master/rest_app/public/ --productid=5b273b0e4bb5995b108b4576 --tokenexport=354c3ddb2bd45aa6e5c4f749749b0b58cbacef48

The main problem is that it is going through the function in controller but not downloading it. And I tried to echo the response it prints code in zip form means a encoded form which have something related to my zip file means its printing the zip not downloading it. I am using Artisan first time. Any idea how to download that file using using user-defined Artisan command.

I am trying this from last two weeks but not able to do anything.

5
Where are you downloading to? I suppose you want to save the file into another location since you're running it in the console.Oluwatobi Samuel Omisakin
yes in local system like in download folderAdesh Kumar
Perhaps you should look at this article. It's a nice walkthrough writing custom artisan commands.joshuamabina
You have shown us your Controller code, which is set up to do browser downloads - setting content headers (relevant only in a browser), reading a file from local disk (only relevant to code running on the remote server), etc. You are asking us about an artisan console command, which is something completely different, but you haven't shown us any of that code. Where is your console code? And remember, re-using the Controller code you've shown us won't work from the console, as I've described.Don't Panic
What I am doing is... exporting the data in json format and put them into one file.... then i put this file to a /temp folder in public folder in zip format... and then trying to download that zip file.... and after downloading deleteing that /temp folderAdesh Kumar

5 Answers

3
votes

How about this:

First identify the file which you want to download and then call

string shell_exec ( string $cmd )

you can use wget to get the file.

for example, in your command

    $fileName = $this->argument('filename');
    $file = url('storage/public/' . $fileName);
    shell_exec("wget $file");
    $this->info('downloading');
1
votes

if you'r using the console, the http download will not work (you are not using a browser).

Instead you can move the file to your wished destination or something like that, but not download it via console

1
votes

console will not handle http response as download (will not save it) I think you should consider to add second argument in your console: --destination=/home/user/downloads/filetosaveas.extension and in your code use curl to download

or more simple use core curl:

curl http://example.com --output my.file --silent
1
votes

It would be nice if you actually went through the nicely done Writing Commands guide at the Laravel Docs site, here.


In a nutshell, there are mainly two ways to register artisan command. Be it that one is an alternative to the other.

  1. (Like routes) specify your instructions within a closure.
  2. (Like controllers) create a command class and specify your instructions.

The closure way

Inside my routes/console.php, I specify something like below:

Artisan::command('download:file', function () {
    // your code goes here.
})->describe('Download a file.');

Now execute a help routine for your newly created command.

-> php artisan help download:file

Description:
  Donwload a file.

Usage:
    donwload:file

//...

The command way

Generate the command class using php artisan make:command EmailFileCommand. This command will create app/Console/Commands/EmailFileCommand.php. Update the file to specify what we want it to do.

// ...
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.
    }
}

Trigger commands via HTTP

Say you want to call the commands via an http request. In my routes/web.php file,

//...
use Artisan;

//...

Route::get('/download', function () {
    Artisan::call('download:file');
});

Route::get('/email', function () {
    Artisan::call('email:file');
});

//...

Now execute your http requests /download and /email.


To download a file, you can use Laravel's Filesystem.

//...
use Storage;

//...

Storage::download('file.jpg')

This post has been extracted from my blog post here.

0
votes

One can use the shell_exec() command to run a terminal command through PHP function or script. It will execute the command without opening the terminal window. the behaviour of command will be little bit different from the original command.