Hakan
Hakan

Reputation: 3885

Copy images between different sites/hosts using PHP

I am planing to build a CMS-system.

The CMS system will be located and fully administrated at: www.mycompany.com/customer

The final website will be located at: www.customer.com

I need to figure out the best way to copy files (eg. images) from: www.mycompany.com/customer/media to: www.customer.com/media

Note: The CMS and customer page will be located on different hosts. And I want to build this function using PHP.

Some thoughts:

The optimal solution would be if the two directories could be cloned automaticly, no matter how the images are uploaded or updated. Maby if there is a way to detect changes to www.mycompany.com/customer/media, then www.customer.com/media could be notifyed about it and send a request to update the image.

A wish would also be that images only could be accessed from www.mycompany.com/customer/media if logged in to the CMS :S

Any tips?

Upvotes: 0

Views: 274

Answers (3)

ajreal
ajreal

Reputation: 47321

You should not use FTP (insecure), or PHP for the replication,
try rsync instead :-

What is rsync ?
http://en.wikipedia.org/wiki/Rsync

rsync is a software application and network protocol for Unix-like and Windows systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. rsync can copy or display directory contents and copy files, optionally using compression and recursion.

In another word, is designed meant for mirroring or replicating (is industry standard)

In general,

  1. setup public key to allow source server to able to ssh into destination server
  2. setup a cronjob in the source server to do rsync

What does the cronjob do ?
In nutshell, it should rsync the selected source directory to destination server,
a quick example :-

* * * * * rsync -avz /home/www.mycompany.com/www $HOST:/home/www.customer.com/www
                     ^ source server directory   ^ destination server, 
                                                   and directory

However, rsync is too hard to describe in few sentences, you can take a look :- http://www.cyberciti.biz/tips/linux-use-rsync-transfer-mirror-files-directories.html (as a start)

Other possibilities is make use of version controlling software, like -:

  1. git
  2. svn

Or make use on CDN (like @Amir Raminfar has mentioned), which itself is already a complete solution for file distribution.

Upvotes: 1

Anthony
Anthony

Reputation: 3218

You can create DB for www.mycompany.com. But host must allows remote access to DB. Create crontab for customer.com which will be check new records within remote DB. This script could be allows for registred users.

You should create htaccess for www.mycompany.com images folder. To allow access from itself and customer.com to download (say copy) images.

In this way all registred users can download any image. It could be wrong for you.

If you need to limit access for each image then you should to store images in DB and generate encrypt url for each image and request. In this case you shold to use remote DB or SOAP service for getting images.

Upvotes: 0

Amir Raminfar
Amir Raminfar

Reputation: 34149

Just make an ftp call or scp call from mycompany.com to customer.com and upload the file.

Alternately, you can use a CDN which is probably a little too much work but it will give you optimal solution and with great performance. You can upload to a common server and have customer.com pull from that website for the images. This gives you the benefit for parallel download for the assets.

Finally, you can run some kind of web service on customer.com that can accept uploads.

I wouldn't do what you suggested which is notification system because it won't be exactly instantaneous and it will result in some time before actually seeing the image.

Upvotes: 0

Related Questions