We have a series of job advertising websites as part of the Find Locally group of sites which, for the time being we have decided to offer as a free service under another set of a dozen or so domains (IT Positions etc).
This was supposed to be a simple project since the code is already written for the Find domains so I just needed to set up the templates for the new sites and then check all the scripts to make sure everything was OK and make a few minor changes.
One of these changes was the way the site will store CVs and documents the jobs advertisers upload which are stored in a hidden directory and then fed out to the user through a session protected downoad script. All this was working fine but since a user could stumble accross a job advert on either a Find site or one of the new sites, I needed to make sure both the advertisers files and the user´s CVs were available to both sites. This wasn´t a problem either as it was just a case of FTP-ing from one to the other and vice-versa when someone uploaded a file. Or so I thought…
After two hours of subtly re-writing the FTP script, checking all the settings, SSH-ing into both servers to see if I could do the FTP upload manually (which worked, dammit!) and checking with other scripts that I´ve used this technique on (which worked, dammit), I was getting very, very wound up (I might have broken a keyboard).
By the time I´d done all this though it had got to the stage where I could either stay for the long haul or call it a night, so I decided to go home and forget about it. I didn´t do this of course, but instead spent the entire bus journey thinking of ways I could do it without using PHP´s FTP functions. That´s when I thought about doing the file transfer over HTTP.
I´ve often used PHP´s file function to trigger a script to run on another domain (e.g. PDF invoice creation) which causes the PHP script to run on the second domain without affecting whats going on at the first domain. Since I knew where the files where to be stored on both domains I figured that I could just use an "file" call to the domain without the document from the domain with the document. The domain without the document could then use fopen and then fwrite to it with the contents of something like this:
fwrite($fp, implode(file(´http://´.$firstDomain.´/´.$pathToFiles.´/´.$file)));
which would pull the file across HTTP as if it was any other download byany web client and then save it to an identical file. The variables used are passed through GET as part of the "file" call and after testing the whole thing… it worked without a problem!!! I never really liked FTP anyway…