Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file from command line in laravel artisan

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.

like image 931
Adesh Kumar Avatar asked Jul 09 '18 08:07

Adesh Kumar


People also ask

What is Laravel artisan command?

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

How do you get artisan command in Laravel?

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.


1 Answers

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');
like image 175
Jagadesha NH Avatar answered Oct 11 '22 02:10

Jagadesha NH