Contact Us

Address: Ground Floor, Bablu Mension, Karigorpara Road, Amjhupi, Meherpur, 7101
Phone: +8801713902426

    How to Transfer Files Server to Server using PHP June 30, 2016|

    How to Transfer Files Server to Server using PHP

    It’s a very common task or daily task to transfer files one server to another. Usually we develop sites to our test domain, show to client and when everything ready here, I transfer files to their server and setup. To transfer files manually much time consuming and bandwidth consuming. FTP client like Filezilla take much times as Filezilla don’t have option to unzip, so have to transfer one by one. I was searching for script that allows file transfer one server to another, which will less time consuming and bandwidth. Thanks David for his great tutorial, tried, that working good, that’s why I am going to share that, which will help you as well me to follow further.

     

    Transfer Files from Server to Server using PHP Copy

    Let’s login to destination server with cPanel or FTP, create a file name oboyob-transfer.php or something like this, and paste these codes to that PHP file, and edit as you needed. Edit this one original file URL ‘http://origin-server-url/files.zip’ and this one for new file name on destination server ‘files.zip’. Then visit http://your-destination-url/oboyob-transfer or whatever you used. This will take time to load, that’s mean its copying your files server to server. After few minutes (depend on your file size) it will show success or failure message. If show success, then check your file manager there is already transferred, if failed please try again later or follow another method below.

    <?php
    /* Source File URL */
    $remote_file_url = 'http://origin-server-url/files.zip';
    
    /* New file name and path for this new file */
    $local_file = 'files.zip';
    
    /* Copy the file from source url to server */
    $copy = copy( $remote_file_url, $local_file );
    
    /* Add notice for success/failure */
    if( !$copy ) {
    echo "Doh! failed to copy $file...\n";
    }
    else{
    echo "WOOT! Thanks Munshi and Munshi IT! success to copy $file...\n";
    }
    ?>

     

    Transfer Files from Server to Server using PHP FTP

    Many server use file protection or if that file protected PHP copy method will not work, so you can try PHP FTP. More or less same as before, now using FTP access. Create oboyob-transfer.php and paste these codes and edit as needed. But read my instruction carefully.

    <?php
    /* Source File Name and Path */
    $remote_file = 'files.zip';
    
    /* FTP Account */
    $ftp_host = 'your-ftp-host.com'; /* host */
    $ftp_user_name = '[email protected]'; /* username */
    $ftp_user_pass = 'ftppassword'; /* password */
    
    /* New file name and path for this new file */
    $local_file = 'files.zip';
    
    /* Connect using basic FTP */
    $connect_it = ftp_connect( $ftp_host );
    
    /* Login to FTP */
    $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
    
    /* Download $remote_file and save to $local_file */
    if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
    echo "WOOT! Thanks Munshi and Munshi IT! Successfully written to $local_file\n";
    }
    else {
    echo "Doh! There was a problem\n";
    }
    
    /* Close the connection */
    ftp_close( $connect_it );
    ?>

    On this code used ftp_get to import the files from source server to destination server. But we can also use ftp_put to export the files (sending the files) from the source server to destination server, use this code:

    <?php
    /* Remote File Name and Path */
    $remote_file = 'files.zip';
    
    /* FTP Account (Remote Server) */
    $ftp_host = 'your-ftp-host.com'; /* host */
    $ftp_user_name = '[email protected]'; /* username */
    $ftp_user_pass = 'ftppassword'; /* password */
    
    /* File and path to send to remote FTP server */
    $local_file = 'files.zip';
    
    /* Connect using basic FTP */
    $connect_it = ftp_connect( $ftp_host );
    
    /* Login to FTP */
    $login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
    
    /* Send $local_file to FTP */
    if ( ftp_put( $connect_it, $remote_file, $local_file, FTP_BINARY ) ) {
    echo "WOOT! Thanks Munshi and Munshi IT! Successfully transfer $local_file\n";
    }
    else {
    echo "Doh! There was a problem\n";
    }
    
    /* Close the connection */
    ftp_close( $connect_it );
    ?>

    After import and export never forgot to delete that php file to prevent other people using it. Enjoy!

    Chat on WhatsApp
    >