#Using `putFileAs()` with `fputcsv()`

8 messages · Page 1 of 1 (latest)

supple charm
#

I'm trying to batch generate several CSV file, so I've made a helper class. I have a common private function CSVLoop which is working fine with my makeResponse() method, but I'm struggiling to understand the parameters I need to be supplying to ->putFileAs():

namespace App\Helpers;

use Illuminate\Support\Collection;
use Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;

class CSV
{
    private static function CSVLoop($file, $columns, $data) :void
    {
        fputcsv($file, array_keys($columns));
        $i = 1;
        foreach ($data as $item) {
            $row = [];
            foreach ($columns as $column => $field) {
                if(is_null($field)) $row[$column] =  "";
                else $row[$column] = $column !== "#" ? $item[$field] : $i;
            }
            fputcsv($file, array_values($row));
            $i++;
        }
        fclose($file);
    }
    public static function makeLocal(String $fileName, String $path, Array $columns, Array | Collection $data) :bool
    {
        $csvFile = tmpfile();
        $csvPath = stream_get_meta_data($csvFile)['uri'];
        $file = fopen($csvPath, "w");
        CSV::CSVLoop($file, $columns, $data);
        Storage::disk("public")->putFileAs($csvPath, $file, $fileName);
        return true;
    }```
supple charm
#

Okay it is working with the SO approach, but not to the path I expected. If I do dd(compact("path", "csvPath", "fileName")); (stupidly updated to 8.2 so can't use Debugbar without the effort of reinstalling and setting up 8.1 until it's updated)

#

Yet this these are the paths being generated:

#

Ah nvm, it's the forward slash at the start

#

At least I thought it was going to be 😦