Advertisement
nocturnalmk

addDocument()

Nov 28th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1.     public function addDocument($title, $date, $file, $subject_id) {
  2.  
  3.         if (!Validation::validTitle($title)) {
  4.             throw new InvalidInputException(Messages::$invalid_title);
  5.         }
  6.        
  7.         if (!Validation::validDate($date)) {
  8.             throw new InvalidInputException(Messages::$invalid_date);
  9.         }
  10.        
  11.         if ($file["name"] == null) {
  12.             throw new InvalidInputException(Messages::$no_file);
  13.         }
  14.        
  15.         if ($file["error"] > 0) {
  16.             $error = $file["error"];
  17.            
  18.             switch ($error) {
  19.                 case 1:  $message = Messages::$file_too_big; break;
  20.                 default: $message = Messages::$system_error;
  21.             }
  22.            
  23.             throw new InvalidInputException($message);
  24.            
  25.         } else {
  26.  
  27.             //$extension = end(explode(".", $file["name"]));
  28.             $extension = strtoupper(substr(strrchr($file["name"],'.'),1));
  29.            
  30.             if (!Validation::validFileType($extension)) {
  31.                 throw new InvalidInputException(Messages::$invalid_extension);
  32.             }
  33.             if ($file["size"] > Config::$max_file_size*1024*1024) {
  34.                 throw new InvalidInputException(Messages::$file_too_big);
  35.             }
  36.                
  37.                     $file["name"] = time()."_".$file["name"];
  38.             move_uploaded_file($file["tmp_name"], "../uploads/" . $file["name"]);
  39.            
  40.             $file_name = $file["name"];
  41.             $size = $file["size"];
  42.  
  43.             $sql = "INSERT INTO documents
  44.                         (title, date, file_name, extension, size, subject_id)
  45.                     VALUES
  46.                         ('$title','$date','$file_name','$extension','$size','$subject_id')";
  47.                
  48.              $result = $this->query($sql);
  49.            
  50.              if (!$result) {
  51.                 throw new DatabaseErrorException(mysql_error());
  52.              }
  53.  
  54.         }
  55.        
  56.         return true;
  57.  
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement