2

I have a website which I'll call website.com that is located on server1. website.com has a field to upload a file. When someone uploads a file on website.com, I don't want the file uploaded to server1, I want it to upload to another server, server2. What is the best way to do this? Can I do this using php, a shell script?

After the file is uploaded to server2, I have a shell script to execute on the file which I will also eventually have to figure out how to run from server1.

I hope this makes sense, thanks in advance.

Corey
  • 771
  • 3
  • 14
  • 32
  • 2
    Can you have the upload form point to server2? – Pekka Aug 08 '11 at 15:54
  • I think the best solution would be to upload the file to server1 temporarily and then server1 sends it to server2. Afterwards server2 does something with that file. Transferring files between servers can be done in various ways. – Cheesebaron Aug 08 '11 at 15:56
  • what are those various ways? server2 can not be accessed through the internet I can only connect to it via SSH. – Corey Aug 08 '11 at 16:20
  • @Corey: You can mount a remote directory from server2 to server1 via ssh with sshfs (http://fuse.sourceforge.net/sshfs.html) and store the files there. – Master_ex Aug 08 '11 at 17:56
  • but how can I automate that mounting process with PHP or a shell script. – Corey Aug 08 '11 at 18:32

2 Answers2

2

another possible way to do this is by uploading this file to your website.com site and use CURL to send the image to another server. once this completes you can remove the image again.

see CURL PHP send image for more information.

-- UPDATE --

For SSH connection you need to install additional libraries in order to allow php to make SSH connection. an excellent tutorial can be found here.

-- UPDATE 2 -- The question intrigued me, so i expanded my research. there seems to be another PHP Library phpseclib around on Sourceforge. In the documentation on page 5 there is some information on how it works.

The only good way to make this to work is to read the image to binary, and send it over the the other server, as text and write that into an file, hence creating an image from the source of the original. Also place the image in a public folder that is accepts calls from your website1 domain, this way you also prevent hot linking your images and saves considerable data.

I also came across this for help with phpseclib.

in the end i wouldnt choose for a solution like this. I would swap your website from server1 to server2, just to keep everything in one place.

Community
  • 1
  • 1
DonSeba
  • 2,098
  • 2
  • 16
  • 25
  • I don't think I can use CURL though because like I said, sever2 is not accessible through the internet. I have to SSH to it – Corey Aug 08 '11 at 16:57
  • The update looks like what I was looking for. It looks very promising, thank you – Corey Aug 08 '11 at 19:00
0

Cant you put the script to handle the upload on Server 2?

You can have your HTML pages with the form served for server 1, but call the PHP for the upload from server 2.

Update For example...

Server 1 has a file index.php which has a form:

<form action='http://server2.com/some_directory/uploader.php' method='POST'>
 .... Some form code
</form>

The form on index.php points to a PHP script on server 2, via a URL. That PHP script can now handle the input.

Of course this will only work if server2 is connected accessible from the internet, if not you will have to use some sort of shell script on server 1 to move the files on the internal network when they are uploaded to server 1.

diagonalbatman
  • 17,340
  • 3
  • 31
  • 31
  • Okay, I think I see what you're saying. How do I call the php function on a different server though? – Corey Aug 08 '11 at 16:11
  • As far as I know server2 is not connected accessible from the internet. I was given server2 for the project I'm working on because it has a lot of space, but as far as I know the only way to connect to it is via SSH – Corey Aug 08 '11 at 16:19
  • Ok then you are probably going to have to look at some sort of "back end" implementation. Now, it depends on if you need to access the uploads in realtime, as to how to proceed. If not then i would probably recomend using a CRON scheduled job to FTP them to sever 2. – diagonalbatman Aug 08 '11 at 16:21
  • I had a feeling this wouldn't be easy :/ The way the site currently works is a file is uploaded and I run some analysis on it with a shell script I wrote and display the output. The problem, which is why I need sever2, is because website.com is actually on a virtual machine on server1. The VM only has 20GB which is why I need server2 with 500GB. I am trying to change everything so that rather than uploading to the local server1 it goes to server2 which has more space. – Corey Aug 08 '11 at 16:28