Created
June 4, 2020 08:19
-
-
Save joelharkes/8cc4aeb55c1de95155f4bff3c1653f70 to your computer and use it in GitHub Desktop.
Upload file in vanilla php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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