Skip to content

Instantly share code, notes, and snippets.

@CydeSwype
Last active March 18, 2023 21:06
Show Gist options
  • Save CydeSwype/7cffbf0db3c3070878d1a0b4d21037cf to your computer and use it in GitHub Desktop.
Save CydeSwype/7cffbf0db3c3070878d1a0b4d21037cf to your computer and use it in GitHub Desktop.
Install SSH2 for PHP 7.1 on MAMP (to support SFTP read/write via file_put_contents, etc.)
// add the repo that has all the PHP goodness
brew tap Homebrew/homebrew-php
// install the ssh2 library
brew install php71-ssh2
// verify you've got ssh2 loaded - should return something like:
// Additional .ini files parsed => /usr/local/etc/php/7.1/conf.d/ext-ssh2.ini
// Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp
// ssh2
// libssh2 version => 1.8.0
// banner => SSH-2.0-libssh2_1.8.0
php -i | grep ssh
// move the so/library to your MAMP directory
cp /usr/local/Cellar/php71-ssh2/1.1.2/ssh2.so /Applications/MAMP/bin/php/php7.1.8/lib/php/extensions/no-debug-non-zts-20160303/
// enable the extension
vim /Applications/MAMP/bin/php/php7.1.8/conf/php.ini
// add this line anywhere in the extensions area
extension=ssh2.so
// then restart MAMP web server and you're good to go.
// visit http://localhost:8888/MAMP/index.php?page=phpinfo&language=English and search
// for ssh to confirm you have the library available
// THEN you can do something like this to write files over SFTP in PHP
file_put_contents("ssh2.sftp://User:Pass@server.com:22/path/on/remote/server/filename.txt", "file contents goes here!");
@pkiula
Copy link

pkiula commented Mar 18, 2023

Doesn't work with PHP 8.2 (installed via MAMP). Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment