Copy files from server to server via php script
Last week I wanted to switch my web hosting server, after getting my new hosting server the most difficult and time taking task was how to accurately and quickly copy files from old server to new server.
Normally we upload and download our website files to/from server using some FTP client like filezilla or fireFTP etc, but when we want to copy files from one online server to another, then it becomes more time taking using FTP client that is first download all files to your local computer and then upload from local computer to new server.
It is almost impossible if you have poor internet connection and large website.
In my case I was going to copy files of about 5 website from old hosting server to new one, some of them were interlinked and files needed to be moved and placed as they were on old server, one single file misplace could lead to big blunder and number of errors.
Firstly I started to download all data from old server, FTP client was showing about 23 hours left, and you know who can wait so long, and even if I have waited for it, then I have to upload it to new server that will also take about full day and night.
I looked for alternatives, some FTP clients which can do this process fast, but all in vain, Then an idea clicked in my mind, that is
The new hosting server will obviously have faster internet connection, if somehow I run FTP client at new server and download file from old server directly to the new server.
but as it was not my personal or dedicated server, so I was not able to install any software over there.
Who can stop the way of Programmer to copy files
I wanted to write my own script in PHP language that can be executed over that hosting server which will work as FTP client. for this task I experimented few scripts, like making real FTP connection using php to copy files, used cURL but but the shortest and easiest way to copy files from server to server was the one given below.
I have copied about 2GB data from my old server to new server in few seconds.
Steps to copy files from server to server
- Goto old server File Manager and zip all files and folder inside public html directory
- Place zipped file say its name is files.zip inside public html folder
- Create a php file copy.php at new server inside public html folder and write the following script inside it
- Goto browser and write new-server.com/copy.php to execute the script
- Go to public html folder at new server you will get files.zip there which is copied from new server, extract it and have fun.
The following is the script to copy file from one server to another
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/*the following 2 lines are not mandatory but we keep it to avoid risk of exceeding default execution time and mamory*/ ini_set('max_execution_time', 0); ini_set('memory_limit', '2048M'); /*url of zipped file at old server*/ $file = 'http://your-domain.com/files.zip'; /*what should it name at new server*/ $dest = 'files.zip'; /*get file contents and create same file here at new server*/ $data = file_get_contents($file); $handle = fopen($dest,"w"); fwrite($handle, $data); fclose($handle); echo 'Copied Successfully.'; |
Hurray, after running this file at new server I got 2GB zipped file copied from old server in few seconds, This is the fastest way to copy or move files between online servers with in few seconds even if you are having poor internet connection even if you are on dailup or 2G mobile connection.
This is copying so fast because it depends on download speed of new server and upload speed of old server which is always faster than normal office of home internet connections.
Note : Do not forget to delete both the zip files at old and new servers after copying and extracting, as these are publicly accessible and someone else can also download it via script like this or normal FTP client.
Hope this helped you, let me know if you have better and optimized alternative to copy files from one server to another, I always appreciate your valuable ideas and views. write it in comments below or mail me.
Comments
I really appreciate your effort and making data exchange so easy.