php - uploading file using OOP -


i new oop , having troubles in transforming codes oop style. not sure if reason because functions written wrongly or im doing wrongly in handlefile.php.

max_size declared

i getting database query failed. message

file.php

<?php  require_once(lib_path . ds . 'database.php');  class file extends databaseobject {  protected static $table_name = "resume"; protected static $db_fields = array('file_id', 'user_id', 'title', 'file_type', 'file_size', 'upload_date', 'status', 'file_data'); public $file_id; public $user_id; public $title; public $file_type; public $file_size; public $upload_date; public $status; public $file_data; private $supportformat = ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf'];  public function title(){     $this->title = $_files["myuploadfile"]["name"]; }  public static function check_size(){     if($_files["myuploadfile"]["size"] < max_size){         return true;     } }  public function extension(){     if($this->file_type == $this->supportformat){         return true;     }     else{         return false;     } }  public function file_data() {     global $database;     $string = file_get_contents($_files["myuploadfile"]["tmp_name"]);     $escaped_data = mysqli_real_escape_string($database->connection, $string);     return $escaped_data; }  protected function attributes() {     // return array of attribute names , values     $attributes = array();     foreach (self::$db_fields $field) {         if (property_exists($this, $field)) {             $attributes[$field] = $this->$field;         }     }     return $attributes; }  protected function sanitized_attributes() {     global $database;     $clean_attributes = array();     // sanitize values before submitting     // note: not alter actual value of each attribute     foreach ($this->attributes() $key => $value) {         $clean_attributes[$key] = $database->escape_value($value);     }     return $clean_attributes; }  public function uploadfile() {     global $database;     $attributes = $this->sanitized_attributes();     $sql = "insert " . self::$table_name . " (";     $sql .= join(", ", array_keys($attributes));     $sql .= ") values ('";     $sql .= join("', '", array_values($attributes));     $sql .= "')";     if ($database->query($sql)) {         $this->id = $database->insert_id();         return true;     } else {         return false;     } } }    $file = new file(); $fl = & $file; ?> 

handlefile.php - handling form

<?php require_once("../../includes/initialize.php"); ?> <?php if (isset($_post['submit'])) { global $file; $file->user_id = $_session['user_id']; $file->title = $_post['title']; $type=$file->extension()  ; $size=$file->check_size()  ; $file->upload_date = date("y-m-d"); $file->status = 1; $data=$file->file_data()  ;  if ($file->uploadfile()) {     //successful message } else {     //unsuccessful message } } ?> 

a part of database.php codes

public function query($sql) {     $result = mysqli_query($this->connection, $sql);     $this->confirm_query($result);     return $result; }  private function confirm_query($result) {     if (!$result) {         die("database query failed.");     } } 


Comments