tags:

views:

87

answers:

4

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?

A: 

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.

Pekka
Care to explain the downvote?
Pekka
The downvote wasn't me, but PHP does support SCP using SSH - see my answer for the links.
Andy Shellam
@Andy good to know, cheers.
Pekka
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 Babuškov
@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.
Pekka
Don't forget about SSH FS, its a very handy (FUSE) way to mount any remote directory from just about anywhere.
Tim Post
@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.
Milan Babuškov
A: 

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.

Andy Shellam
+2  A: 

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
Milan Babuškov
A: 

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.

Tim Post