Skip to content

Instantly share code, notes, and snippets.

@ActuallyConnor
Created January 20, 2021 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ActuallyConnor/e7a80af7672c948f4bb070f9f32dab72 to your computer and use it in GitHub Desktop.
Save ActuallyConnor/e7a80af7672c948f4bb070f9f32dab72 to your computer and use it in GitHub Desktop.
SFTP with PHP Sec Lib
<?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