Benjamest's avatar

Create CSV and upload to SFTP

Hello,

I have an array which I'm trying to convert to a temp CSV file to upload to an SFTP server (using "league/flysystem-sftp": "~1.0" on Laravel 8.12). The code I have so far is below.

    $filename = "40079_STOCK_".Carbon::now()->format("dmYHi").".csv";
     $fp = fopen('php://temp', 'w');

    foreach ($csvArray as $row)
    {
        fputcsv($fp, $row);
    }

    fclose($fp);

    Storage::disk('sftp')->put($filename, $fp);

The problem I have is that I'm getting an error from vendor\phpseclib\phpseclib\phpseclib\Net\SFTP.php:2033

    ErrorException
    strlen() expects parameter 1 to be string, resource given

Can someone point me in the direction of what I'm doing wrong or whether there is a better way to achieve this?

Thanks

0 likes
2 replies
Snapey's avatar

take away all the other code and just try sending a file

Sounds like maybe your sftp credentials are not expressed correctly

1 like
Benjamest's avatar
Benjamest
OP
Best Answer
Level 6

Thanks @snapey . I had a play around with it including saving it to the local disk which failed. It turns out the below line destroys the file before I can put it. Deleting the line solved the issue.

    fclose($fp); 

Please or to participate in this conversation.