如何使用 PHP 在服务器之间移动文件


How to move a file between servers using PHP

我需要创建一个 PHP 函数来在 2 台服务器之间移动文件......例如,我有 Sever1 和 Server2,我有一个 file1.php位于 Server1 上,我需要执行一个 PHP 函数,例如:server1/execute.php将 file1.php 移动到 Server2 上的特定目录。

我正在使用:

<?php
$filename="file1.php";
unlink("ftp://USER:PASSWORD@IPADDRESS/httpdocs/DESTINATIONFOLDER/".$filename);
if(!copy($filename, "ftp://USER:PASSWORD@IPADDRESS/httpdocs/DESTINATIONFOLDER/".$filename))  
{echo "error";}
?>    

该代码在我的本地服务器上工作,但是当我在服务器上发布时,文件复制不完整。

一些想法?

如果文件太大,无法在分配给脚本的时间内复制,则复制将被中断。

使用set_time_limit(0);删除时间限制。请注意,不能在所有服务器配置上执行此操作。

要采取的另一种措施是在传输结束时检查文件大小,像这样,您可以在发生错误并且仅部分上传时处理这种情况。

我认为您必须打开 PHP 中的allow_url_fopen.ini并将 755 permision 设置为源文件。 并且您必须有权访问 目标文件夹 .

或者使用类似的东西:http://stephenmcintyre.net/blog/copy-images-allow_url_fopen-off

好吧,您可以使用ftp传输文件。使用以下简单易用的代码来执行此操作。

/**
* Transfer (Export) Files Server to Server using PHP FTP
*/
/* 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 );