Copy files from one server to another using php – Transfering or copying file is our regular work in the web development area. There are many reasons to transfer or copy our file from one server to another server. some of are
1) May Need to change hosting server and our total site need to transfer another hosting server.
2) Maybe the current server is not much good and we want to get better service and want to copy our total site.
3) There are many of out there.

Copy files from one server to another using php

So how can I transfer my file from one server to another server?
There are also some possible solutions:
Solution-1: We may use FTP(File Transfer Protocol) to transfer the file from one server to another server. But you need to create an FTP account in both servers. And first, have to download all files from our current server and have to upload in the new server. Two processes are equally time-consuming and boring.
Solution-2: you can make Zip file and download it via Cpanel and upload it to the new server. It will be easier but need to do the work two times first, download and then upload the file in new server.
Solution-3: You can user CURL. Which is fast and secure but need to tech knowledge little bit because curl code is not easy to read by anyone.
I recommend the best solution which is PHP Copy() function. Copy files from one server to another using PHP is easy and extremely fast.

Copy files from one server to another using PHP

Step by step process
Step -1: Login your current server’s Cpanel and locate the folder and files which you want to transfer to another server. Select all file and folder and click right button and click zip. Your file and folder will be zip within a short time. The time depends on your server performance and file size. Now get the total URL of the zip file. Like ‘http://wordpress.org/latest.zip’
Step -2: Login your new server Cpanel and make a file named ‘myupload.php’
Now open the file in edit mode and paste the following code.

<?php 
/* copy file form one server to another using PHP */

$source = 'http://yourdomain.com/mysite.zip';

$target= 'mysite.zip';
if(copy($source,$target)){
echo 'File transfered Successfully';

}else{
echo 'Could not copy file!! Please try again and check the source !!';
}

Step 3: now run the code on your new server. Write the file address in the address bar like http://mynewdomainsite.com/myupload.php
and hit enter. And see the magic. within a very short time file will be in your new server. magic right 🙂  ?

Copy files from one server to another using PHP

Code Explanation: The value of the variable $source will be your old server and zip file address. Like “http://wordpress.org/latest.zip”. Be sure you address is correct. You can check by writing the code in the address bar. If it is correct then zip file will start downloading.
$target value is your file name. You can change it later if you want. The script should be in your new server. All file (old server zip and new script file) address have to correct otherwise it will not work.
If everything is correct as I explain, it will just work magically.

So now you know, how to Copy files from one server to another using PHP easily and shortly.
Thank you to reading the article. See you soon with some interesting PHP and WordPress goods. Stay connect with www. yourwordpressexpert.com and get the fresh content of WordPress and PHP.