write to database not working with upload image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
iamalex
Forum Newbie
Posts: 5
Joined: Thu Aug 12, 2010 10:11 am

write to database not working with upload image

Post by iamalex »

I am trying to write to database when image uploads. Image uploads but will not write to database.

Code attached.

Can someone help.

On windows machine, localhost.

Thks, Alex

functions used below:

Code: Select all


class functions {
 
    protected static $table_name="users";
    protected static $db_fields = array('id', 'username', 'password', 'first_name', 'last_name');
                                        
    public $id;
    public $username;
    public $password;
    public $first_name;
    public $last_name;

protected function sanitized_attributes() {
    global $database;
    $clean_attributes = array();
    foreach($this->attributes() as $key => $value){
        $clean_attributes[$key] = $database->escape_value($value);
    }
    return $clean_attributes;
}

public function create() {
  global $database;
  $attributes = $this->sanitized_attributes();
  $sql = "INSERT INTO ".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;
 }
 
}
}
photograph.php below:

Code: Select all

<?php

require_once(LIB_PATH.DS.'initialize.php');   //functions files

class Photograph extends DatabaseObject {
  protected static $table_name="photographs";
  protected static $db_fields = array('id', 'filename', 'type', 'size', 'caption');
  public $id;
  public $filename;
  public $type;
  public $size;
  public $caption;
    
    private $temp_path;  
    protected $upload_dir="images";
          public $errors=array();
    
protected $upload_errors = array(
// errors here 
);


public function attach_file($file) {

if(!$file || empty($file) || !is_array($file)) {
    
    $this->errors[] = "No file was uploaded.";
    return false;
} elseif($file['error'] != 0) {

$this->errors[] = $this->upload_errors[$file['error']];
return false;
} else {


$this->temp_path    = $file['tmp_name'];
$this->filename     = basename($file['name']);
$this->type         = $file['type'];
$this->size         = $file['size'];

return true;
        }
}


public function save() {

if(isset($this->id)) {

    $this->update();
} else {



if(!empty($this->errors)) { return false; }


if(strlen($this->caption) > 255) {
    $this->errors[] = "The caption is too long.";
    return false;
    
}

if(empty($this->filename) || empty($this->temp_path)) {
    $this->errors[] = "The file location not available.";
    return false;
}


$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;


if(file_exists($target_path)) {
    $this->errors[] = "The file {$this->filename} exists.";
   return false;
}


if(move_uploaded_file($this->temp_path, $target_path)) {

if($this->create()) {
    unset($this->temp_path);
    return true;
}
}  else {

$this->errors[] = "The file upload failed.";
return false;

    }

  }  


 }  

}  
?>
Post Reply