Page 1 of 2
How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 8:12 am
by simonmlewis
Hi
We are about to start building a tool so that a customer who has two websites independently hosted, can 'import' products from one site to another, including the primary photo.
It won't import the categories, because of their differing IDs and names.
I know how to connect to the other server database, and obviously therefore gather the filename of the primary photo. But not sure how to then get hold of it's file, and copy it over (presumably using some sort of FTP transfer?)....?
Any ideas would be great.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 9:44 am
by requinix
Moving stuff to a CDN would be nice. Then the backend is irrelevant.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 10:01 am
by simonmlewis
Code: Select all
$result = mysql_query("SELECT photoprimary FROM products WHERE id = '$id'", $db);
while ($row = mysql_fetch_object($result))
{
/* Source File URL */
$remote_file_url = (dirname(__DIR__). '\images\productphotos\$row->photoprimary');
/* New file name and path for this file */
$local_file = (dirname(__DIR__). '\images\productphotos\$row->photoprimary');
}
/* 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";
}
This is not being liked very much!
[text]Warning: copy(C:\xampp\phpMyAdmin\site-wide\images\productphotos\$row->photoprimary): failed to open stream: Invalid argument in C:\xampp\phpMyAdmin\site-wide\includes\a_import.inc on line 128
Notice: Undefined variable: file in C:\xampp\phpMyAdmin\site-wide\includes\a_import.inc on line 132
Doh! failed to copy ... [/text]
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 10:06 am
by requinix
There are at least three problems with that code. The most significant is the fact that the source and destination paths are the same.
In other words the code doesn't make sense.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 10:10 am
by simonmlewis
Oh yes of course.
Bearing in mind this is locally done, how do I tell the dirname section to go back further, so I can enter the FULL destination?
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 10:38 am
by requinix
Just use the full path. If you really don't want to, stack more dirname()s on top.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 11:03 am
by simonmlewis
Code: Select all
$resultfinal = mysql_query("SELECT photoprimary FROM products WHERE id = '$id'", $db);
while ($rowfinal = mysql_fetch_object($resultfinal))
{
/* Source File URL */
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\$rowfinal->photoprimary';
$photoprimary = $rowfinal->photoprimary;
}
/* New file name and path for this file */
$local_file = 'C:\xampp\phpMyAdmin\site-wide2\images\productphotos\$photoprimary ';
/* 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 $remote_file_url...\n";
}
else{
echo "WOOT! success to copy $remote_file_url...\n";
}
[text]Warning: copy(C:\xampp\phpMyAdmin\site-wide\images\productphotos\$rowfinal->photoprimary): failed to open stream: Invalid argument in C:\xampp\phpMyAdmin\site-wide\includes\a_import.inc on line 132
Doh! failed to copy C:\xampp\phpMyAdmin\site-wide\images\productphotos\$rowfinal->photoprimary... [/text]
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Fri May 19, 2017 6:19 pm
by Christopher
Single quotes do not parse embedded variables. For embedded array and property references -- use braces.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sat May 20, 2017 5:17 am
by simonmlewis
Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';
or
Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\. {$rowfinal->photoprimary} .';
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sat May 20, 2017 5:38 am
by Celauran
simonmlewis wrote:Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';
or
Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\. {$rowfinal->photoprimary} .';
Neither.
Code: Select all
$remote_file_url = "C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}";
or
Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\' . $rowfinal->photoprimary};
Also bear in mind that you may need to escape your backslashes.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sat May 20, 2017 5:43 am
by Celauran
A few other things to bear in mind.
1. You're currently hard-coding the location of these files. That's not going to hold up for your client as their absolute paths are different. You'll need to either programmatically get the full paths or use paths relative to the document root of the sites in question.
2. You're copying files locally, while it sounds like your client requires files be sent from one server to another server, requiring an entirely different approach. requinix has a good idea when he suggested using a CDN for both sites. Barring that, you'll want to look into something like scp or rsync or the like. Will need to be a network call regardless, and not a local file copy operation.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sun May 21, 2017 4:36 am
by simonmlewis
Yes this is a local call for now, to test the transfer of files.
So how does one do it from one Hosted server to another? I have all the passwords for FTP - are they needed or can it simply be done the way I have here, but with the website address?
I don't want to need to install anything - or do I have to??
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sun May 21, 2017 8:59 am
by Celauran
FTP doesn't make sense here. Don't even think of it in terms of pushing a file from one server to the other. Rather, since these are public images, just make a cURL request (or Guzzle or whatever) from the server that needs the file to the server that has it. You don't need to worry about directory structure on the remote system either, just call the URL and save the response wherever you like.
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Sun May 21, 2017 1:23 pm
by simonmlewis
So think of it more like 'saving' a file other than copying it?
I don't know Guzzle.
That Copy code - will that not work?
Re: How do I copy files across, using PHP - host 1 > host 2?
Posted: Mon May 22, 2017 4:36 am
by simonmlewis
Code: Select all
$resultfinal = mysql_query("SELECT photoprimary FROM products WHERE id = '$id'", $dbid);
while ($rowfinal = mysql_fetch_object($resultfinal))
{
/* Source File URL */
$remote_file_url = 'C:/\xampp/\phpMyAdmin/\site-wide/\images/\productphotos/\' . $rowfinal->photoprimary;
$photoprimary = $rowfinal->photoprimary;
}
/* New file name and path for this file */
$local_file = 'C:/\xampp\phpMyAdmin/\site-wides/\images/\productphotos/\' . $photoprimary;
/* 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 $remote_file_url...\n";
}
else{
echo "WOOT! success to copy $remote_file_url...\n";
}
This is the code now, and it has an error:
[text]Parse error: syntax error, unexpected 'C' (T_STRING) in C:\xampp\phpMyAdmin\site2-wide\includes\a_import.inc on line 127[/text]