How do I copy files across, using PHP - host 1 > host 2?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

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?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I copy files across, using PHP - host 1 > host 2?

Post by requinix »

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?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I copy files across, using PHP - host 1 > host 2?

Post 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.
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?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I copy files across, using PHP - host 1 > host 2?

Post by requinix »

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?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
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?

Post by Christopher »

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?

Post 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} .';
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I copy files across, using PHP - host 1 > host 2?

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I copy files across, using PHP - host 1 > host 2?

Post 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.
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?

Post 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??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I copy files across, using PHP - host 1 > host 2?

Post 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.
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?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
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?

Post 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]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply