• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Writing a file to disk on another machine on my bsd "network"


My client has 2 co-located machines behind a router/whatever at ISP. One server receives email, and parses it with a content-filter wtitten in PHP. The other server will act as a "file server".

I need the PHP script on the email server to be able to save the attachments in the email to a directory on the file server.

I can extract the attachments without a problem, however I looking for whats the best way to write files to the other machine.

The boxes are running FreeBSD 4.9. Both have an internet routable IP [the "file server" also acts as a web server, but this may change in future].

What would you guys recommend as the best way to handle this problem?

Many thanks in advance
2 Solutions
NFS (Network File System) would probably be a good solution. You can mount a partition from one machine on the other and write to it as if it was a local directory.

You could use NFS, but for something like this it's way simpler to just use HTTP PUT.

On the file server that you want to put files on, compile mod_actions into Apache and use the Script directive (http://httpd.apache.org/docs/mod/mod_actions.html#script) to handle HTTP PUT uploads with a PHP script like this:

$root = "/www/your.file.server";
      die("Invalid request method.");
if (getenv("REMOTE_ADDR") != "")
      die("Access denied.");

function mkdir_recursive($pathname, $mode = 0777) {
      if (is_dir($pathname))
            return true;
            if (!mkdir_recursive(dirname($pathname), $mode))
                  return false;
      return mkdir($pathname, $mode);

$filename = substr($_SERVER["REQUEST_URI"], 1);
$in = fopen("php://input", "r");
$out = @fopen($filename, "w");
if (!$out || !flock($out, LOCK_EX))
      die("Couldn't lock destination file.");
while ($data = fread($in, 4096))
      fwrite($out, $data);

Then, on the server that is writing the files, compile curl (http://php.he.net/curl) into PHP and use a simple script like this to upload files:

function http_put($source, $dest) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $dest);
      curl_setopt($ch, CURLOPT_PUT, 1);
      curl_setopt($ch, CURLOPT_INFILE, fopen($source, "r"));
      curl_setopt($ch, CURLOPT_INFILESIZE, filesize($source));
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $response = curl_exec($ch);
      return $response;

http_put("/tmp/somefile.txt", "http://your.file.server/files/somefile.txt");
I think you can do a ftp function and write your file into a specific directory on that machine which run a ftpd !
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now