Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fen's avatar
Level 2

Create csv file then upload to remote SFTP (solved)

------update------

It works after I added 'root' => 'uploads/' in sftp setting, as I set up permit in uploads folder in my SFTP server.

I also moved the headers within the case not sftp.

Hi,

I am trying to create a csv file then upload it to remote server:

        $mode = 'sftp';  
        // output headers        
        header('Content-Encoding: UTF-8');
        header('Content-Type: text/csv; charset=utf-8');

        if ($mode == 'sftp') {
            header("Content-disposition: filename=" . $file_name . ".csv");
            $output = fopen('php://temp', 'w');
        } else {
            header('Content-Disposition: attachment; filename=' . $file_name . '.csv');
            $output = fopen('php://output', 'w');
        }

        foreach ($rows as $row) {
            fputs($output, implode(';', $row));
            fputs($output, PHP_EOL);
        }
        fclose($output);

        if ($mode == 'sftp') {
            if (Storage::disk('do_sftp')->put($file_name . '.csv', $output)) return 'Upload success'; 
            return 'Upload failed';
        }
        // digital ocean 
        'do_sftp' => [
            'driver' => 'sftp',
            'host' => 'xxx',        
            'username' => 'xxx',
            'password' => 'xxx',
            'privateKey' => env('PRIVATE_KEY_PATH'),
            'port' => 22,			
        ],

It fails and I cannot figure out why...

Thank you in avance for your opinions.

0 likes
1 reply

Please or to participate in this conversation.