5

I have a script on my one server and I want that script to create a file on another server of mine using PHP, NOT VIA FTP?

1
  • What servers are those? What platform? Commented Feb 18, 2010 at 11:33

4 Answers 4

2

There are many ways to do this. I'd pick the first one myself because it's easiest to set up:

  • If you have PHP+Apache on another server, just call some script on the other server using file_get_contents with http URL as filename or use cURL if you need to POST file contents as well.
  • If the servers are in same network (LAN, VPN) you can use Windows shares/Samba or NFS to mount a remote directory to you local filesystem and simply write to file directly using fopen/fwrite functions
  • Use SSH via SCP or SFTP
Sign up to request clarification or add additional context in comments.

Comments

1

PHP allows sending files across SSH - see the ssh2* family of functions, in particular ssh2_scp_send and ssh2_scp_recv.

I've never used them myself, but the infrastructure is there in Linux, just like SMB in Windows.

Comments

0

In general, FTP is the only regularly and easily available way (in PHP) to create a file on another server.

There are of course other protocols that enable you to create a file, but they all require installation of software on either one or both servers:

  • Samba (would enable access to the remote server through an absolute file path)
  • WebDaV (PHP client libraries available)
  • SCP (Finding a PHP client is probably going to be hard)

If both servers run PHP, it's probably the easiest to set up a PHP script on the remote server that accepts file data trough POST, and writes it out to a local file. Not a perfect solution, though, due to the limits usually imposed on POST uploads.

5 Comments

The downvote wasn't me, but PHP does support SCP using SSH - see my answer for the links.
In general, FTP is not really available everywhere and poses a huge security hole. And all other solutions you mention outperform it. I also wonder why you say that HTTP post is not very performance oriented when it has least overhead of all other solutions?
@Milan You are right in regards to performance, but there are usually very strict limitations in place in regard to file size. This is not an optimal solution, and any of the others is preferable IMO.
Don't forget about SSH FS, its a very handy (FUSE) way to mount any remote directory from just about anywhere.
@Pekka: if you use HTTP POST, there are no limitations. I upload files sized 500MB or more using that method. To utilize HTTP POST you can use PHP's cURL functions for example.
0

You could always use DAV, but it might require some configuration on the receiving server. There is also SSHFS, which lets you easily mount the remote directory locally over a SSH tunnel, or just use the ssh2_* family of functions as Andy Shellam suggested.

Really, there are lots of ways to accomplish this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.