Friday, June 23, 2017

Transfer Files Server to Server Using Simple PHP

Hello.

Here i explained to move/migrate files to another server/hosting, and you/your client only have FTP access to the server. And to download these files and re-upload to another server can take a lot of time using FTP client such as Filezilla. FTP do not have zip – unzip functionality, so you need to upload it one by one. And server to server transfer is a lot faster than downloading and uploading the files. You can use this simple PHP script to move files from one server to another server.

Note: It’s just a simple examples. you need to build your own auth/security method if needed.


1. Using PHP Copy to move files from server to server.

You can just create a php file in the destination server and load the file once in your browser. For example you add this code in http://destination-url/copy-files.php and in copy-files.php you add this php code:

  1. /**
  2.  * Transfer Files Server to Server using PHP Copy
  3.  *
  4.  */
  5.  
  6. /* Source File URL */
  7. $remote_file_url = 'http://origin-server-url/files.zip';
  8.  
  9. /* New file name and path for this file */
  10. $local_file = 'files.zip';
  11.  
  12. /* Copy the file from source url to server */
  13. $copy = copy( $remote_file_url, $local_file );
  14.  
  15. /* Add notice for success/failure */
  16. if( !$copy ) {
  17.     echo "Oh! failed to copy $file...\n";
  18. }
  19. else{
  20.     echo "Done! success to copy $file...\n";
  21. }

2. Using PHP FTP to move files from server to server

  1. /**
  2.  * Transfer (Import) Files Server to Server using PHP FTP
  3.  */
  4. /* Source File Name and Path */
  5. $remote_file = 'files.zip';
  6.  
  7. /* FTP Account */
  8. $ftp_host = 'your-ftp-host.com'; /* host */
  9. $ftp_user_name = 'ftp-username@your-ftp-host.com'; /* username */
  10. $ftp_user_pass = 'ftppassword'; /* password */
  11. /* New file name and path for this file */
  12. $local_file = 'files.zip';
  13. /* Connect using basic FTP */
  14. $connect_it = ftp_connect( $ftp_host );
  15. /* Login to FTP */
  16. $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass ); 
  17.  
  18. /* Download $remote_file and save to $local_file */
  19. if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
  20.     echo "Done! Successfully written to $local_file\n";
  21. }
  22. else {
  23.     echo "Oh! There was a problem\n";
  24. }
  25. /* Close the connection */
  26. ftp_close( $connect_it );
 using FTP you have more flexibility, the code above is using ftp_get to import the files from source server to destination server. But we can also use ftp_put to export the files (send the files) from source server to destination, using this code:

  1. /**
  2.  * Transfer (Export) Files Server to Server using PHP FTP
  3.  */
  4. /* Remote File Name and Path */
  5. $remote_file = 'files.zip';
  6. /* FTP Account (Remote Server) */
  7. $ftp_host = 'your-ftp-host.com'; /* host */
  8. $ftp_user_name = 'ftp-username@your-ftp-host.com'; /* username */
  9. $ftp_user_pass = 'ftppassword'; /* password */
  10. /* File and path to send to remote FTP server */
  11. $local_file = 'files.zip';
  12. /* Connect using basic FTP */
  13. $connect_it = ftp_connect( $ftp_host );
  14. /* Login to FTP */
  15. $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
  16. /* Send $local_file to FTP */
  17. if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
  18.     echo "WOOT! Successfully transfer $local_file\n";
  19. }
  20. else {
  21.     echo "Doh! There was a problem\n";
  22. }
  23. /* Close the connection */
  24. ftp_close( $connect_it );

To make it easier to understand:
  • ftp_connect is to connect via FTP.
  • ftp_login is to login to FTP account after connection established.
  • ftp_close is to close the connection after transfer done (log out).
  • ftp_get is to import/download/pull file via FTP.
  • ftp_put is to export/send/push file via FTP.
After you import/export the file, always delete the PHP file you use to do this task to prevent other people using it.

ZIP and UNZIP Files using PHP

Of course to make the transfer easier we need to zip the files before moving, and unzip it after we move to destination.

ZIP Files using PHP

You can zip all files in the folder using this code:
  1. /**
  2.  * ZIP All content of current folder
  3.  */
  4. /* ZIP File name and path */
  5. $zip_file = 'files.zip';
  6. /* Exclude Files */
  7. $exclude_files = array();
  8. $exclude_files[] = realpath( $zip_file );
  9. $exclude_files[] = realpath( 'zip.php' );
  10. /* Path of current folder, need empty or null param for current folder */
  11. $root_path = realpath( '' );
  12. /* Initialize archive object */
  13. $zip = new ZipArchive;
  14. $zip_open = $zip->open( $zip_file, ZipArchive::CREATE );
  15.  
  16. /* Create recursive files list */
  17. $files = new RecursiveIteratorIterator(
  18.     new RecursiveDirectoryIterator( $root_path ),
  19.     RecursiveIteratorIterator::LEAVES_ONLY
  20. );
  21. /* For each files, get each path and add it in zip */
  22. if( !empty( $files ) ){
  23.  
  24.     foreach( $files as $name => $file ) {
  25.  
  26.         /* get path of the file */
  27.         $file_path = $file->getRealPath();
  28.  
  29.         /* only if it's a file and not directory, and not excluded. */
  30.         if( !is_dir( $file_path ) && !in_array( $file_path, $exclude_files ) ){
  31.  
  32.             /* get relative path */
  33.             $file_relative_path = str_replace( $root_path, '', $file_path );
  34.  
  35.             /* Add file to zip archive */
  36.             $zip_addfile = $zip->addFile( $file_path, $file_relative_path );
  37.         }
  38.     }
  39. }
  40.  
  41. /* Create ZIP after closing the object. */
  42. $zip_close = $zip->close();

UNZIP Files using PHP

You can unzip file to the same folder using this code:
  1. /**
  2.  * Unzip File in the same directory.
  3.  * @link http://stackoverflow.com/questions/8889025/unzip-a-file-with-php
  4.  */
  5. $file = 'file.zip';
  6.  
  7. $path = pathinfo( realpath( $file ), PATHINFO_DIRNAME );
  8.  
  9. $zip = new ZipArchive;
  10. $res = $zip->open($file);
  11. if ($res === TRUE) {
  12.     $zip->extractTo( $path );
  13.     $zip->close();
  14.     echo "Done! $file extracted to $path";
  15. }
  16. else {
  17.     echo "Oh! I couldn't open $file";
  18. }

No comments:

Post a Comment