Created
January 20, 2021 18:16
-
-
Save ActuallyConnor/e7a80af7672c948f4bb070f9f32dab72 to your computer and use it in GitHub Desktop.
SFTP with PHP Sec Lib
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
<?php | |
// composer require phpseclib | |
include get_stylesheet_directory() . "/vendor/autoload.php"; | |
use phpseclib\Net\SFTP; | |
function sftp() { | |
$sftp = new SFTP( SFTP_HOST ); // Stored in wp-config.php (also 1Password) | |
if ( !$sftp->login( SFTP_USER, SFTP_PW ) ) { // Stored in wp-config.php (also 1Password) | |
throw new Exception( 'Login failed' ); | |
} | |
if ( $sftp->file_exists( 'dir_name' ) ) { // check if dir exists | |
$sftp->chdir( 'dir_name' ); // cd into dir | |
} | |
if ( $sftp->file_exists( 'file_name.zip' ) ) { // check if file exists | |
$sftp->get( 'file_name.zip', wp_upload_dir()['basedir'] . '/file_name.zip' ); // download file to uploads base dir | |
} | |
// Unzip zip file | |
try { | |
unzip_file( wp_upload_dir()['basedir'] . '/file_name.zip', wp_upload_dir()['basedir'] . '/file_name.html' ); // unzip file and put contents somewhere | |
} catch ( Exception $e ) { | |
error_log( $e ); // error log if unsuccessful in unzipping | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment