PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun May 28, 2017 3:39 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Fri May 19, 2017 8:12 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 9:44 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
Moving stuff to a CDN would be nice. Then the backend is irrelevant.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 10:01 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
        $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!
Syntax: [ Download ] [ Hide ]
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 ...

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 10:06 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 10:10 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 10:38 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6423
Location: WA, USA
Just use the full path. If you really don't want to, stack more dirname()s on top.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 11:03 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
        $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";
}


Syntax: [ Download ] [ Hide ]
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...

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
PostPosted: Fri May 19, 2017 6:19 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13396
Location: New York, NY, US
Single quotes do not parse embedded variables. For embedded array and property references -- use braces.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat May 20, 2017 5:17 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';

or
Syntax: [ Download ] [ Hide ]
$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.


Top
 Profile  
 
PostPosted: Sat May 20, 2017 5:38 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6243
Location: Montreal, Canada
simonmlewis wrote:
Syntax: [ Download ] [ Hide ]
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}';

or
Syntax: [ Download ] [ Hide ]
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\. {$rowfinal->photoprimary} .';

Neither.

Syntax: [ Download ] [ Hide ]
$remote_file_url = "C:\xampp\phpMyAdmin\site-wide\images\productphotos\{$rowfinal->photoprimary}";

or
Syntax: [ Download ] [ Hide ]
$remote_file_url = 'C:\xampp\phpMyAdmin\site-wide\images\productphotos\' . $rowfinal->photoprimary};

Also bear in mind that you may need to escape your backslashes.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sat May 20, 2017 5:43 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6243
Location: Montreal, Canada
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.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun May 21, 2017 4:36 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Sun May 21, 2017 8:59 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6243
Location: Montreal, Canada
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.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Sun May 21, 2017 1:23 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Mon May 22, 2017 4:36 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4203
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
        $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:

Syntax: [ Download ] [ Hide ]
Parse error: syntax error, unexpected 'C' (T_STRING) in C:\xampp\phpMyAdmin\site2-wide\includes\a_import.inc on line 127

_________________
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group