I have two websites. On one my clients can upload images they want processed. Now I want to find a way to copy all the uploaded images from server one to server two using php and cron.
Is this possible? If so how?
I have two websites. On one my clients can upload images they want processed. Now I want to find a way to copy all the uploaded images from server one to server two using php and cron.
Is this possible? If so how?
A much more efficient way would be if both the servers had a shared area where the files would be uploaded.
Ideally, your client's script should upload the images to a shared folder on your second server since that is the one which does all the processing.
Then your second server can process the images and save the output to a different folder.
Copying files between 2 servers via PHP, I believe, is a very inefficient solution as you would be wasting server resources on both the servers.
If you want the client's images on both the servers without copying, have the client's code POST the data to a script on your server as well and let your server script save the image to a temp folder and maybe have that same script process it and generate the output thus eliminating the need to have a copy, processing and cron script.
<?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
}
?>
The following are the methods for this:
First, Using PHP Copy to move files from server to server. You can just create a php file in the destination server and load the file once in your browser.
$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";
}
Second, Using PHP FTP to move files from server to server.
But sometimes using PHP Copy didn’t work if the files is somehow protected by this method (hotlink protection maybe?). For example, the source is from Hostgator it didn’t work.
But we can use another method. Using FTP (in PHP) to do the transfer using the code:
$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 );
using FTP you have more flexibility, the code above is using ftp_get to import the files from source server to destination server. But we can also use ftp_put to export the files (send the files) from source server to destination, using this code:
$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 );
To make it easier to understand:
If you want copy single file:
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
for multiple copy:
$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));
}
Source server path should have permission to copy.