Skip to content

Instantly share code, notes, and snippets.

@joelharkes
Created June 4, 2020 08:19
Show Gist options
  • Save joelharkes/8cc4aeb55c1de95155f4bff3c1653f70 to your computer and use it in GitHub Desktop.
Save joelharkes/8cc4aeb55c1de95155f4bff3c1653f70 to your computer and use it in GitHub Desktop.
Upload file in vanilla php
protected function putfile(string $path, string $content)
{
$host = config('services.sftp.host');
$port = config('services.sftp.port');
$con = ssh2_connect($host, $port, [], ['disconnect' => fn () => Log::debug('sftp disconeccted')]);
if (false === $con) {
throw new HttpException(503, 'sftp connection failed', ['sftpHost' => $host, 'sftpPort' => $port]);
}
if (false === ssh2_auth_password($con, config('services.sftp.username'), config('services.sftp.password'))) {\
ssh2_disconnect($con);
throw new HttpException(503, 'sftp authentication failed', ['sftpHost' => $host, 'sftpPort' => $port]);
}
$sftp = ssh2_sftp($con);
$stream = fopen("ssh2.sftp://$sftp/$path", 'w');
fwrite($stream, $content);
fclose($stream);
ssh2_disconnect($con);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment