Kjetil's Information Center: A Blog About My Projects

Web-based Paste Server Script

Sometimes I need to transfer text from my Android smart phone to my Linux PC. There are many many ways to do this I guess, but I wanted something easy under my control. All phones have web browsers, so I settled on the classic solution of simply running a PHP script served by an Apache HTTP server. The script just displays the input form, and on a POST request saves the contents to a timestamped file i the /tmp/ directory on the PC.

Here is the XHTML compliant PHP script:

<?php 
  header('Content-type: application/xhtml+xml; charset=utf-8');
  print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Paste server!</title>
  </head>
  <body>
<?php
  print "      <form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\">\n";
?>
        <p>
          <input type="submit" value="submit" />
          <br />
          <textarea id="content" name="content" rows="24" cols="80"></textarea>
        </p>
      </form>
    <p>
<?php
  if (isset($_POST["content"])) {
    $filename = "/tmp/paste_" . date("H:i:s") . ".txt";
    $content = $_POST["content"];
    $fh = fopen($filename, "a");
    fwrite($fh, $content);
    fclose($fh);
    print "      Pasted " . strlen($content) . " bytes to: ". $filename . "\n";
  }
?>
    </p>
  </body>
</html>
          


Topic: Scripts and Code, by Kjetil @ 17/07-2026, Article Link