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.
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
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)
- setup public key to allow source server to able to ssh into destination server
- 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 -:
Or make use on CDN (like @Amir Raminfar has mentioned), which itself is already a complete solution for file distribution.