File transfer: Client>Server>Server

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!

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

File transfer: Client>Server>Server

Post by Parody »

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?

Thanks :D
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: File transfer: Client>Server>Server

Post by Syntac »

Try sending a raw HTTP request from the middle server via fsockopen().
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: File transfer: Client>Server>Server

Post by Syntac »

Parody wrote:Wouldn't that still require that the file was sent through the middle server?
Yes.
Parody wrote:The request could not be made until the data in the request (the file) had been received could it?
No. You can't send a file that hasn't been transferred.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

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?
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: File transfer: Client>Server>Server

Post by Syntac »

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:

Code: Select all

$file = file_get_contents( $_FILES["file"]["tmp_name"] );
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

I suppose that is the only way. Thanks for your help Syntac
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: File transfer: Client>Server>Server

Post by mikelbring »

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.
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: File transfer: Client>Server>Server

Post by VladSun »

You may use a SFTP mounted directory and put the uploaded files in it.
http://fuse.sourceforge.net/sshfs.html

This way, the PHP script processing the uploaded files will "see" it like an ordinary local directory :)
There are 10 types of people in this world, those who understand binary and those who don't
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

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 :D

Thanks everyone
cboileau
Forum Newbie
Posts: 8
Joined: Sun Nov 02, 2008 8:28 am

Re: File transfer: Client>Server>Server

Post by cboileau »

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 :D

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).
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Re: File transfer: Client>Server>Server

Post by Parody »

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?
Post Reply