Page 1 of 1
Uploading a single file to multiple servers?
Posted: Tue May 11, 2004 5:05 pm
by SleepyP
As in the subject line, the eventual goal is to upload a single file from the client to multiple servers all of which host the same receiver file (perhaps upload.php?). is there a way to code a form which generates an HTTP POST and sends it to a variable list of URLs stored in a string array?
as far as i can tell, the only way to get a valid POST from an HTML form is using a File input box. so far i haven't been able to figure out how to re-use the same value from that box or set the default value of a File field. as a result i haven't worked out a way to re-use a single File field to send the file to multiple servers.
are there alternative ways to generate a valid HTTP POST? i wouldn't think so since that would (i thought) require client-side functions to get attributes on the file to be uploaded. anyone have any advice on this one?
Posted: Tue May 11, 2004 5:10 pm
by markl999
Sounds like a job for the server rather than the client. I'm not sure of the exact answer, or even if replicating the form post is the answer, but
http://dodds.net/~cardinal/sendtohost.txt might point you in the right direction. It basically 'fakes' a form post, so you could post to one server and have that server 'fake' the form post to the other servers. I might have got the wrong end of the stick but it sounds like a kludge for replication *shrug*
Posted: Tue May 11, 2004 7:08 pm
by SleepyP
it will definitely have to be done by the server once the client uploads the file one time (thus providing enough information for the server to effectively POST to other servers). at this time i'm not entirely clear how that function works but i'll play around with it and see what i can figure out...
Posted: Tue May 11, 2004 7:32 pm
by launchcode
You could approach this from the other angle - i.e. your client uploads the file once to one server which then in turn kicks off scripts on the other servers and they grab the file from that initial server - pass them the filename/id or similar and they can retrieve it via a standard fopen call as long as they know where to look.
Cheers,
Rich
Posted: Wed May 12, 2004 2:41 am
by SleepyP
thats actually probably a lot easier to code. the only thing i'm not really up on is using PHP to move files around on a server. a script could sit in the root of a server and be used to download the file in question but what do you actually use to move the downloaded file from the root to a specific subdir? i'm still learning file-handling in PHP so any help you could lend would be very appreciated

Posted: Wed May 12, 2004 6:38 am
by launchcode
Once uploaded you should use: move_uploaded_file() to get the temporary file (the one the user just uploaded) to its final destination. Providing it has sufficient write permissions on that location, it will work fine.
Posted: Wed May 12, 2004 7:03 am
by qads
another way of doing this is to make a function which FTPs the file to all the servers, which is also easy to do

.
Posted: Thu May 13, 2004 8:27 pm
by SleepyP
umm will move_uploaded_file() work on a file downloaded by the same PHP script?
my servers are Apache HTTP 1.3 with PHP installed, so i don't think they will respond to FTP stuff, but i dunno. i wasn't planning on making this package use FTP functions though....
Posted: Fri May 14, 2004 5:47 am
by launchcode
move_uploaded_file() does pretty much what its name implies - it moves a file that the USER has uploaded. That's all. Nothing more.
So if you want Joe Bloggs sat at his browser to upload a file, and then for your script to all that file to be "downloaded" by your other servers - you need to move it somewhere accessible first, and that's where the move_uploaded_file function comes into play.
After that point the OTHER servers can either get the file via the FTP functions (which will work fine with your set-up) or via something like fopen().
Posted: Fri May 14, 2004 12:40 pm
by SleepyP
what are you talking about? i guess we are suffering a breakdown of communications.
my basic upload script is done. it works just fine. i have a single PHP document which generates a dynamic form the user uses to specify the target directory and server and a receiving script which resides on each server and catches the uploaded file and a URL-passed parameter specifying the directory the uploaded file goes to. i was planning on using this to get the file to one of the servers on the array and then trigger a sequence of automated downloads by the other servers of the file.
that (the downloading part) is the portion of the code i am working on now, and as such i was thinking you meant i should use move_uploaded_file() in the process of the other servers downloading the file, which wouldn't work at all.
see, when you said "FTP" functions i assumed you meant somehow using file transfer protocol. i was under the impression that doing so required a server to be running some sort of FTP service. i assume now that when you say 'FTP" you don't mean functions which attempt to log-in to an FTP service and make use of it but instead PHP-based HTTP functions which move files around. is this correct?
also, to clarify, what i'm not certain about now is how i go about making a PHP script which resides at the root of a server and downloads a file from the internet and puts it into a specific sub-directory. i will do some more research on the matter...
one other question which is related but completely different:
is there a way for PHP to determine the current IP address or domain name for the server the script is running on? like if i have a script running on
http://www.script1.com, is there a way to initialize a variable with a string containing either "
www.script1.com" or the IP address assigned to that domain? being able to do this will make the automated file propogation script easier...
-EDIT- i found that using the function copy() seems to work just fine for downloading files, and it lets you specify the destination directory, so it should be perfect for my purposes...
Posted: Fri May 14, 2004 12:50 pm
by xisle
It doesn't need to be that complicated really. The uploading and FTPing to multiple servers can be done in one script on the server you are uploading to.
Posted: Fri May 14, 2004 12:51 pm
by launchcode
No, when I said FTP I meant FTP. If all the other servers have FTP access to the one with the file, there's no reason at all why it wouldn't work.
You cannot MOVE a file via HTTP. It is impossible. All you can do is request that file and you can do that via fopen() and then save it out locally.
The way I had imagined it working would quite simply be:
1) Client uploads file to Server A
2) Server A saves file to location within the web root
3) Server A activates download scripts on Servers B, C and D by simply calling the script on those servers via fopen.
4) Servers B,C & D in turn request the file from Server A via fopen (having been told where it lives with part 3 above) and save the file out locally.
It can't see it being any more complex than that.
As for obtaining the domain/IP of the server - sure, it's in the $_SERVER super global. Dump out phpinfo() on your server to see it (and loads of other useful stuff): <?php phpinfo(); ?> will do.
Posted: Fri May 14, 2004 1:20 pm
by SleepyP
thanks for that, i have everything i need to finish my array admin tool...
so let me ask you this: if i have a server which is running only the standard distribution of Apache HTTP Server 1.3 with PHP 4 installed, is it possible to access that server with FTP?
Posted: Fri May 14, 2004 1:50 pm
by launchcode
If that is ALL it's running, no. You need an FTP server as well. But most (all?) Unix servers have them as standard and they're easy enough to find for Windows boxes too.
Posted: Fri May 14, 2004 7:09 pm
by SleepyP
for this project the specs are to use just PHP, so it's a little harder than normal i guess... anyways, i just wanted to post something i just learned:
the Copy() function works well for downloading a file from anywhere online to the local server, but it will fail unless you are careful about formatting the strings you feed it. the source string (if it is a URL) must be properly encoded, meaning all spaces replaced with %20, etc. however, the destination path must be exactly as the path exists on the server, meaning all spaces must be left as is, etc...
this minor detail messed me up for a good hour today.....