Upload files from one server to another server if php doesn't work?

I've tried in many ways to upload files using FTP functions in PHP. But that doesn't work for me. I don't know what mistake I have made in this code. I also gave the path to a local file or other file, but it doesn't work in either case. I have given my code below. Can anyone help find my problem?

/* Source File Name and Path */
$backup_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';
//$backup_file = '/workspace/all-projects/artbak/assets/uploads/edi/test.txt';
$remote_file = $backup_file;

$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */ 

/* New file name and path for this file */
$local_file = '/public_html/example.txt';

/* 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_put( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
    echo "WOOT! Successfully written to $local_file\n";
}
else {
    echo "Doh! There was a problem\n";
}

/* Close the connection */
ftp_close( $connect_it );

      

The code below works for local upload to server, but does not work for server upload to server. It shows that the server is not connecting. Please give some ideas to the guys. I struggle with this concept more.

$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */ 

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

$file = "dummy.txt";

$remote_file = "receiveDummy.txt";

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

      

+3


source to share


1 answer


Thanks for the support. I am happy to post this answer in a couple of days of work. You just need to avoid the target server path /public_html/test.txt

instead test.txt

. If you have any subfolders, enter sample/test.txt

. Please check the below code, use it for someone like me.

/* Source File Name and Path */
            $remote_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';

            $ftp_host = 'hostname'; /* host */
            $ftp_user_name = 'username'; /* username */
            $ftp_user_pass = 'password'; /* password */ 

            /* New file name and path for this file */
            $local_file = 'test.txt';

            /* 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_put( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
                echo "WOOT! Successfully written to $local_file\n";
            }
            else {
                echo "Doh! There was a problem\n";
            }

            /* Close the connection */
            ftp_close( $connect_it );

      



And I have a firewall block on one server so only the first ftp is not connecting via php code. Now I am solving this problem. Because my code running on local server already knows. For this I have named the link below to solve the firewall block on the server, Unable to FTP with PHP ftp_connect from localhost . So it works well.

0


source







All Articles