Php 在两个Web服务器之间复制文件夹中的所有文件
我有两个网站。在其中一个平台上,我的客户可以上传他们想要处理的图像。现在我想找到一种方法,使用php和cron将所有上传的图像从服务器1复制到服务器2 这可能吗?如果是这样,怎么做?Php 在两个Web服务器之间复制文件夹中的所有文件,php,Php,我有两个网站。在其中一个平台上,我的客户可以上传他们想要处理的图像。现在我想找到一种方法,使用php和cron将所有上传的图像从服务器1复制到服务器2 这可能吗?如果是这样,怎么做? <?php $source = 'http://www.testing.com/abc'; $destination = $_SERVER['DOCUMENT_ROOT'] . '/test/'; if (copy($source , $destination )) { //File copied
<?php
$source = 'http://www.testing.com/abc';
$destination = $_SERVER['DOCUMENT_ROOT'] . '/test/';
if (copy($source , $destination )) {
//File copied successfully
}else{
//File could not be copied
}
?>
如果两台服务器都有一个文件上传的共享区域,则效率更高
理想情况下,客户端脚本应该将图像上传到第二台服务器上的共享文件夹中,因为这是完成所有处理的服务器
然后,您的第二台服务器可以处理图像并将输出保存到其他文件夹
我认为,通过PHP在两台服务器之间复制文件是一种效率非常低的解决方案,因为这会浪费两台服务器上的服务器资源
如果您希望客户端的图像在两台服务器上都不复制,请让客户端的代码将数据也发布到服务器上的脚本中,并让服务器脚本将图像保存到临时文件夹中,可能让相同的脚本处理图像并生成输出,从而无需复制,处理和cron脚本。以下是执行此操作的方法: 首先,使用PHP Copy将文件从服务器移动到服务器。您只需在目标服务器中创建一个php文件,然后在浏览器中加载该文件一次
$remote_file_url = 'http://origin-server-url/files.zip';
// New file name and path for this 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! success to copy $file...\n";
}
其次,使用PHP FTP将文件从服务器移动到服务器
但如果文件以某种方式受到此方法的保护(可能是热链接保护?),则有时使用PHP Copy不起作用。
例如,源代码来自Hostgator,但它不起作用
但我们可以使用另一种方法。使用FTP(在PHP中)使用以下代码进行传输:
$remote_file = 'files.zip';
/* FTP Account */
$ftp_host = 'your-ftp-host.com'; /* host */
$ftp_user_name = 'ftp-username@your-ftp-host.com'; /* username */
$ftp_user_pass = 'ftppassword'; /* password */
/* New file name and path for this 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! Successfully written to $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
使用FTP您有更大的灵活性,上面的代码使用FTP_get将文件从源服务器导入到目标服务器。但我们也可以使用ftp_put将文件从源服务器导出(发送文件)到目标服务器,使用以下代码:
$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 );
为便于理解,请执行以下操作:
- ftp_connect是通过ftp进行连接李>
- ftp_登录是在建立连接后登录到ftp帐户李>
- ftp_close是在传输完成(注销)后关闭连接李>
- ftp_get是通过ftp导入/下载/拉取文件李>
- ftp_put是通过ftp导出/发送/推送文件
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
对于多个副本:
$directory = '/path/to/my/directory';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
foreach($scanned_directory as $file_image) {
$src = $file_image;
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
}
源服务器路径应具有复制权限。ftp\sftp将是明显的可选择副本