PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Build csv and upload to google drive in php (laravel 5.8)

 March 08, 2022     laravel, php     No comments   

Issue

How do I send a response()->stream() to google drive ? I'm not getting it because this method returns a class and not a file. My question is if I will need to save locally using file_put_contents() so that I can then send it to google drive

public function buildCsv($columns, $content): \Closure
    {
        return function () use ($columns, $content){
            $file = fopen('php://output', 'w');
            fputcsv($file, $columns);

            foreach ($content as $item) {
                fputcsv($file, $item);
            }

            fclose($file);
        };
    }
$cb = $this->buildCsv($this->CSVColumns, $csvData);
\Storage::disk('google')->put("csv-test", response()->stream($cb, 200, $headers));

On google drive my file looks like this: Google drive file


Solution

Try this

public function buildCsvFile($columns, $content): string
{
    $file = tmpfile();
    fputcsv($file, $columns);
    
    foreach ($content as $item) {
        fputcsv($file, $item);
    }
    $metaDatas = stream_get_meta_data($file);
    return file_get_contents($metaDatas['uri']);
}
$cb = $this->buildCsv($this->CSVColumns, $csvData);
\Storage::disk('google')->put("csv-test.csv", $cb);


Answered By - Krolexer
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing