How do I copy files across, using PHP - host 1 > host 2?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
How do I copy files across, using PHP - host 1 > host 2?
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.
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I copy files across, using PHP - host 1 > host 2?
Moving stuff to a CDN would be nice. Then the backend is irrelevant.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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";
}[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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I copy files across, using PHP - host 1 > host 2?
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.
In other words the code doesn't make sense.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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?
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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I copy files across, using PHP - host 1 > host 2?
Just use the full path. If you really don't want to, stack more dirname()s on top.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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";
}Doh! failed to copy C:\xampp\phpMyAdmin\site-wide\images\productphotos\$rowfinal->photoprimary... [/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How do I copy files across, using PHP - host 1 > host 2?
Single quotes do not parse embedded variables. For embedded array and property references -- use braces.
(#10850)
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\. {$rowfinal->photoprimary} .';Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I copy files across, using PHP - host 1 > host 2?
Neither.simonmlewis wrote:orCode: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';Code: Select all
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\. {$rowfinal->photoprimary} .';
Code: Select all
$remote_file_url = "C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}";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?
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.
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.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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??
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??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do I copy files across, using PHP - host 1 > host 2?
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.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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?
I don't know Guzzle.
That Copy code - will that not work?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do I copy files across, using PHP - host 1 > host 2?
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";
}[text]Parse error: syntax error, unexpected 'C' (T_STRING) in C:\xampp\phpMyAdmin\site2-wide\includes\a_import.inc on line 127[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.