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

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 »

Because of the backslashes, you've got mismatched quotes, leading to that syntax error. Also, C:\stuff isn't a URL, it's a path on a filesystem.
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 »

I tried it with just \, and that failed too. Or is escaping them doing like so:

\\

Oh and I know. Just a figure of speech!
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 »

Yes, you need to escape backslashes with other backslashes.
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 »

Ok that worked - i think.
I deleted a copy of the file being copied over and ran it again, and locally it seems to have worked. But not convinced my code that checks on the two databases are all working. So need to check that really.

And then, see how to make that script work in a live environment. In theory, I just use the website address rather than the c:\ string ??
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 »

Trying to see how to make the right sort of connection here, to multiple databases (some will not be on the same server).

For now, locally:

Code: Select all

  $dbuk = new PDO('mysql:dbname=site_uk;host=localhost', 'root', '');
  $dbie = new PDO('mysql:dbname=site_ie;host=localhost', 'root', '');

       $query = $dbuk ->prepare ("SELECT * FROM products WHERE id = '$dbie '");
         while ($row = $query->fetch(PDO::FETCH_OBJ)) 
           {

}
This doesn't work. Probably as nothing is being executed, but am I close?
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 »

I've sorted this bit now.
I forgot that this whole site has one core connection, so I am using that for the local one, and then a separate $pdo-query for the other site.
Then I just call it in the query at the start variable, rather than at the end.
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 »

What sort of query string would it be for the file location?
Would it just be the http://..... or the hosted file location within the filing structure?
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 »

If you're trying to retrieve a file from a remote host, use the URL, not the file path. http://some.site.com/assest/images/foo.jpg or whatever.
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 »

But if saving it, as in copying over, the 'pick up' point is the URL, but the storage string will be a properly file string won't it, which I'll need to get from the host.
Unless there is a clever way of me finding out what it is?
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 »

Wouldn't you already know? I mean, you're requesting a specific image, and you're basing that request on something. You've got logic on how/where to store images for your products, you know which product you're requesting an image for... Am I missing something here?
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 »

I am copying data from a database - that's easy.
I am copying one image from a location. I know the location from it's URL.
But it needs to be copied *to* somewhere. This is on a live hosting server. I am not a 'server' person, so I don't know the 'raw' file string of where that is stored. It won't be the URL of the local website, or would it simple me $filestring = /images/productphotos/ which is how I echo the images??
<img src='/images/productphotos/$row->photoprimary' />.

Is it as straight forward as that??
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 »

Sure, you're getting the filename from the database, but that also contains information about what that image belongs to; product, category, etc. You're retrieving the image from a different location, but the logic to save it should be the same as for a newly uploaded image.
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 »

I don't think you get my issue here.
The image contains none of that. It's a file.
I just need to tell PHP *where the file needs to be placed*.
It's current location differs from it's copied destination.
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 »

I guess I don't get your issue. I assumed you were simply retrieving missing images for existing entries in the database.
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 »

I am just copying an image from one site to another.
I'm running an INSERT script with the data from another database. That's fine.
I have located the file on the other site. That's fine.
But it's the new storage destination that I am stuck on. It might be something I have to ask me hosts about.
How does the script know where to store it - that's the issue. The physical folder.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply