Solved

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

Posted on 2004-04-28
6
209 Views
Last Modified: 2008-02-01
Hi,

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
Mark
0
Comment
Question by:mh2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 17

Accepted Solution

by:
dorward earned 250 total points
ID: 10937189
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.

http://www.chrisranjana.com/freebsd-handbook-documentation/network-nfs.html
http://www.onlamp.com/pub/a/bsd/2002/02/14/Big_Scary_Daemons.html
0
 
LVL 2

Assisted Solution

by:chrisbolt
chrisbolt earned 250 total points
ID: 10939874
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";
if ($_SERVER["REQUEST_METHOD"] != "PUT")
      die("Invalid request method.");
if (getenv("REMOTE_ADDR") != "1.2.3.4")
      die("Access denied.");

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

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

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);
      curl_close($ch);
      return $response;
}

http_put("/tmp/somefile.txt", "http://your.file.server/files/somefile.txt");
?>
0
 

Expert Comment

by:hqtrung
ID: 10944038
I think you can do a ftp function and write your file into a specific directory on that machine which run a ftpd !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question