When it comes to moving files securely between servers, using SCP in PHP is a solid choice. This method uses the Secure Copy Protocol (SCP) to transfer files over SSH, ensuring everything stays safe during the process. In this guide, I’ll show you how to set it up and use it, so you can get files where they need to be without any headaches.
Table of Contents
What you will need
Before diving into the setup, make sure your environment has these:
- PHP Installed: Your server should have PHP installed. If you’re not sure, run
php -v
to check. - SSH2 Extension: PHP needs the SSH2 extension enabled for this to work. Use
phpinfo()
to confirm it’s active. If not, you’ll need to install it.
How to Install the SSH2 Extension
If the SSH2 extension isn’t installed yet, here’s a quick way to get it running:
For Linux
- Open your terminal and run this command to install the required libraries:
sudo apt-get install libssh2-1-dev libssh2-php
- Add the extension to your
php.ini
file:extension=ssh2.so
- Restart your server:
sudo service apache2 restart
For Windows
- Download the right DLL file for your PHP version from PECL.
- Move the DLL file into your PHP
ext
directory (usually something likeC:\php\ext
). - Edit your
php.ini
file and add this line:extension=php_ssh2.dll
- Restart your web server to apply the changes.
Copy Files from Local to Remote Server
Now, let’s transfer a file from your local server to a remote server using SCP in PHP.
- Start by creating an SSH connection.
$connection = ssh2_connect('remote.server.com', 22); ssh2_auth_password($connection, 'username', 'password');
Replace'remote.server.com'
with your remote server’s address, and fill in your username and password. - Once connected, use
ssh2_scp_send()
to send the file:ssh2_scp_send($connection, '/local/path/to/file.txt', '/remote/path/to/file.txt', 0644);
Update the paths to point to your file’s location and the destination on the remote server.
Copy Files from Remote to Local Server
Want to copy files the other way around? No problem. Here’s how you can bring a file from a remote server to your local machine.
- Just like before, start with the SSH connection:
$connection = ssh2_connect('remote.server.com', 22); ssh2_auth_password($connection, 'username', 'password');
- Then, use
ssh2_scp_recv()
to pull the file:ssh2_scp_recv($connection, '/remote/path/to/file.txt', '/local/path/to/file.txt');
Make sure the paths reflect where your file is located and where you want to save it.
Use SSH Keys for Better Security
Passwords work, but using SSH keys for authentication is safer and more convenient. With key-based authentication, you won’t need to type your password every time.
Generate SSH Keys
- On your local computer, open a terminal and run this:
ssh-keygen -t rsa -b 4096
- It will generate two files: a private key and a public key.
- Copy the contents of your public key to the remote server’s
~/.ssh/authorized_keys
file.
Update Your PHP Code
When using keys, update your connection code like this:
$connection = ssh2_connect('remote.server.com', 22);
ssh2_auth_pubkey_file($connection, 'username', '/path/to/id_rsa.pub', '/path/to/id_rsa');
Final Thoughts
Copying files with SCP in PHP doesn’t have to be complicated. Whether you’re using passwords or SSH keys, the SSH2 extension provides a reliable way to get the job done. With the steps in this guide, you’ll have a secure and smooth file transfer process ready to go.
FAQ
What is SCP in PHP?
SCP (Secure Copy Protocol) in PHP is a way to securely transfer files between servers over SSH using the SSH2 extension.
Do I need the SSH2 extension to use SCP?
Yes, the SSH2 extension is required to use SCP functions like ssh2_scp_send()
and ssh2_scp_recv()
.
Can I use SSH keys instead of passwords?
Absolutely. SSH keys are more secure and eliminate the need to enter a password for every connection.
What if my server doesn’t have the SSH2 extension?
You’ll need to install it. Check the installation instructions in this guide for Linux or Windows setups.
Is SCP in PHP suitable for large file transfers?
Yes, but for very large files, consider optimizing your server and network settings for better performance.
I know this is an old post, but just in case…I am getting this error, and I have tried to install the dll and everything I have read about, on both flavours, 64 and 32, but no avail. I can’t get SSH to show up on phpinfo.
Fatal error: Call to undefined function ssh2_connect()
That is the error I am getting. I am running PHP 5.6.5
Thank you
Hello I am also getting a “Fatal error: Call to undefined function ssh2_connect()” message.
Running PHP 7 within a WordPress 4.9 environment.