在远程服务器上上传文件的最佳方式

时间:2018-04-10 08:09:03

标签: php file upload

我需要将文件从[SERVER A]上传到[SERVER B] (相同的服务器,但不同的环境/子域)

我试图找到最好的方法:

1)在[SERVER A]上传我的文件,然后使用FTP协议将其放在[SERVER B]上?

2)直接在[SERVER B]执行上传脚本? (但对于这个,我不知道该怎么做

3)也许是另一种解决方案?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用:

fopen和fwrite

<?php
set_time_limit(0); //Unlimited max execution time

$path = 'newfile.zip';
$url = 'http://example.com/oldfile.zip';
$newfname = $path;
echo 'Starting Download!<br>';
$file = fopen ($url, "rb");
if($file) {
    $newf = fopen ($newfname, "wb");
    if($newf)
        while(!feof($file)) {
            fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            echo '1 MB File Chunk Written!<br>';
        }
}
if($file) {
    fclose($file);
}
if($newf) {
    fclose($newf);
}
echo 'Finished!';
?>

<强> FTP

<?php
/**
 * Transfer (Export) Files Server to Server using PHP FTP
 * @link https://shellcreeper.com/?p=1249
 */

/* Remote File Name and Path */
$remote_file = 'files.zip';

/* FTP Account (Remote Server) */
$ftp_host = 'your-ftp-host.com'; /* host */
$ftp_user_name = 'ftp-username@your-ftp-host.com'; /* 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! Successfully transfer $local_file\n";
}
else {
    echo "Doh! There was a problem\n";
}

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