Location: PHPKode > projects > Simple Document Management System > update.php
<?php
  require('lib/config.inc.php');
  require('lib/auth.inc.php');
  require('lib/classes.inc.php');
  require('lib/functions.inc.php');

  function upload_failed($message) {
    // Trash it.
    @unlink($_FILES['userfile']['tmp_name']);

    echo "<h2 align=\"center\">Error: $message</h2>\n";
    print_footer();
    exit;
  }

  /*
   * Basic input validation.
   */
  $doc_id = intval($_REQUEST['doc_id']);
  $info   = mysql_real_escape_string($_REQUEST['info']);

  $user = new user($_SESSION['login']);
  $document = new document($doc_id);

  print_header("Updating Document #$document->id");

  if(!isset($_FILES['userfile']))
    upload_failed("Document was not found");

  if($_FILES['userfile']['name'] != $document->name )
    upload_failed("Document should be called $document->name");

  if(!file_exists($_FILES['userfile']['tmp_name']))
    upload_failed("Document was not uploaded");

  $fp = fopen($_FILES['userfile']['tmp_name'], "r");
  if(!$fp)
    upload_failed("Cannot open uploaded document");
  $content = fread($fp, $_FILES['userfile']['size']);
  fclose($fp);
  unlink($_FILES['userfile']['tmp_name']);

  $res = @mysql_query("REPLACE INTO documents_content(id,content) VALUES($document->id,'". base64_encode($content) ."')");

  switch( mysql_errno() ) {

    // Updated OK.
    case 0:
        echo "<h2 align=\"center\">Updated ". htmlspecialchars(mysql_real_escape_string($_FILES['userfile']['name'])) ." ({$_FILES['userfile']['size']} bytes) to revision ". ($document->revision + 1) ."</h2>\n";

        @mysql_query("UPDATE documents SET revision = revision+1 WHERE id=$document->id");
        @mysql_query("UPDATE documents SET modified = NOW() WHERE id=$document->id");

        // New info?
        if($info != NULL && $info != "") {
            @mysql_query("REPLACE INTO documents_info(id,info) VALUES($document->id,'". $info ."')");
            if(mysql_errno()) {
                echo "<h3 align=\"center\">New info <u>not</u> saved<br>". mysql_error() ."</h3>\n";
            } else {
                echo "<h3 align=\"center\">New info saved</h3>\n";
            }
        }

        // New keywords?
        if($keywords != NULL && $keywords != "") {
            // Delete the old keywords.
            @mysql_query("DELETE FROM documents_keywords WHERE id=$document->id");
            $keywords = ereg_replace(",", " ", $keywords);
            $keywords = ereg_replace("  ", " ", $keywords);
            $keywords = explode(" ", $keywords);
            $keyword = current($keywords);
            echo "<h3 align=\"center\">Using keywords: \n";
            do {
                @mysql_query("INSERT INTO documents_keywords(id,keyword) VALUES($document->id,'". mysql_real_escape_string($keyword) ."')");
                if(mysql_errno())
                    echo "<br>Error, $keyword not saved\n";
                else
                    echo "<br>$keyword\n";
            } while ($keyword = next($keywords));
            echo "</h3>\n";
        }
        break;

    default:
        upload_failed( "Could not save updated content<br>Error: ". mysql_error() );
        break;
  }

  print_footer()

?>
Return current item: Simple Document Management System