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!
I'm attempting to write a file transfer script which allows a server to upload a file provided by a client to another server.
Basically the file needs to go from the client to the end server, but the rest of the form needs to be sent to the 'middle' server. I realise that uploading the file to the middle server from the client and then transferring it to the end server would work (can't explain the technicalities though), albeit extremely inefficient. The most efficient way I can see of doing this would be that the middle server simply directs the file without actually transferring it.
My idea of how this should work is as follows:
The file is selected by the client and the form submitted
The form is sent to the middle server, but the file is sent to the end server with other variables from the middle server without being uploaded/saved to the middle server
The end server then sends a response to the middle server
I use CURL for some other stuff and I guess this could be done with it, could someone let me know how they would go about doing this?
Wouldn't that still require that the file was sent through the middle server? The request could not be made until the data in the request (the file) had been received could it?
At the moment I'm looking for the most efficient way of doing this, but this might be the only way.
I understand what you're saying, and it seems the most logical method, but it requires that the data is sent twice. Surely that is slow and extremely inefficient and although it might be alright for small files, but not for large ones.
Is there no way to direct the browser to transfer the file to a different destination before the file is transferred?
1] The only thing more efficient is to send it all to the same place which, if I interpreted your original post correctly, is not an option.
2] I don't think so. An HTTP request has to be transferred in its entirety before the server can take any action... So no, you have to send it all to the middle server.
Not sure how you'd go about getting the file's contents in order to send, but something along the lines of the following:
Look into using FTP functions. I have users upload a file to the current server then I use php ftp functions to transfer it to the remote server and delete it off the local server.
How does the process you use work? Does the file get transferred by the same script which uploads the file from the client or is it initiated afterwards?
Thanks for the pointer, I'll use that if what I'm about to suggest won't work.
At the moment my service receives requests and data via HTTP for ease of use and it would be extremely useful if I could essentially convert a whole file into a string (like opening it with notepad) and send it via HTTP just like a piece of text for interpretation by my service and then convert it back into a file when requested. This would also remove the need to check the file as it could not be executed on the server in any shape or form.
I'm trying to keep my service simple and uniform (which might not be efficient, but will be easy to manage). I'm also trying to avoid using FTP or SSH as they seem to complicate matters in my opinion as user accounts and physical folders will need to be created and things get unnecessarily complex.
If you think this won't work for some reason then let me know before I begin
Parody wrote:Thanks for the pointer, I'll use that if what I'm about to suggest won't work.
At the moment my service receives requests and data via HTTP for ease of use and it would be extremely useful if I could essentially convert a whole file into a string (like opening it with notepad) and send it via HTTP just like a piece of text for interpretation by my service and then convert it back into a file when requested. This would also remove the need to check the file as it could not be executed on the server in any shape or form.
I'm trying to keep my service simple and uniform (which might not be efficient, but will be easy to manage). I'm also trying to avoid using FTP or SSH as they seem to complicate matters in my opinion as user accounts and physical folders will need to be created and things get unnecessarily complex.
If you think this won't work for some reason then let me know before I begin
Thanks everyone
Honestly the way that I would do it is to do this like a file upload, but instead of copying the temp file to a perm location, just read the file and send it off to the other server. You'll need to transfer the file to server 1 before it can be sent to server 2. You can also store binary files in SQL databases if it fits your purposes (BLOB).
I understand that what you are suggesting is a more widely used conventional method and if setup correctly and securely would be more efficient, but is there any reason why what I am suggesting would not work?
It's as simple as sending a string so it can be attached onto other textual data being sent, is as secure as sending encrypted text and allows the end server to store it without any security issues in a database.
My questions are now:
How do I convert between a string and a file? Can I just use fopen()? and is there any pitfalls such as different character sets or such which may corrupt the files being sent?