我有两个网站。首先,我的客户可以上传他们想要处理的图像。现在,我想要找到一种方法,使用php和cron将所有上传的图像从服务器1复制到服务器2。
这个是可能的吗?如果是这样的话,是怎么做的?
发布于 2017-04-05 13:26:24
一种更有效的方法是,如果两台服务器都有一个共享区域,文件将在其中上传。
理想情况下,客户端的脚本应该将图像上传到第二台服务器上的共享文件夹,因为这是执行所有处理的服务器。
然后,您的第二台服务器可以处理图像并将输出保存到不同的文件夹。
我认为,通过PHP在两台服务器之间复制文件是一种非常低效的解决方案,因为这会浪费两台服务器上的服务器资源。
如果您希望在两台服务器上都不复制客户端的图像,请让客户端的代码也将数据发送到服务器上的脚本,并让服务器脚本将图像保存到临时文件夹,并可能让相同的脚本对其进行处理并生成输出,从而消除复制、处理和cron脚本的需要。
发布于 2017-04-05 13:25:18
<?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
}
?>发布于 2017-04-05 13:27:46
以下是实现此目的的方法:
首先,使用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 );为了更容易理解,请执行以下操作:
https://stackoverflow.com/questions/43222394
复制相似问题