Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Why is my function not a string in my php code??

Hi, I am getting this error message:

Fatal error: Function name must be a string in /hermes/web09c/b2950/moo.auroriellacom/includes/images.php on line 144

but I have no idea what it is refering to.  Does anyone see something that I missed??
<?php
require_once 'includes/database.php';
require_once 'includes/user.php';

class Image extends Database_Object { 

protected static $table = "images";
// list of database fields
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();

// Common Databaes Methods
/*
$upload_errors = array(
	// http://www.php.net/manual/en/features.file-upload.errors.php
	UPLOAD_ERR_OK			=> "No errors.",
	UPLOAD_ERR_INI_SIZE		=> "Larger than upload_max_filesize.",
	UPLOAD_ERR_FORM_SIZE	=> "Larger than form MAX_FILE_SIZE.",
	UPLOAD_ERR_PARTIAL		=> "Partial upload.",
	UPLOAD_ERR_NO_FILE		=> "No file.",
	UPLOAD_ERR_NO_TMP_DIR	=> "No temporary directory.",
	UPLOAD_ERR_CANT_WRITE	=> "Can't write to disk.",
	UPLOAD_ERR_EXTENSION	=> "File upload stopped by extension."
);
*/
// Pass in $_FILE(['uploaded_file']) as an argument
public function attach_file($file) {
	// Perform error checking on the form parameters
	if(!$file || empty($file) || !is_array($file)) {
		// error: nothing uploaded or wrong argument usage
		$this->errors[] = "No file was uploaded.";
		return false;
	}
	elseif($file['error'] != 0) {
		// error: report what PHP says went wrong
		$this->errors[] = $this->upload_errors[$file['error']];
		return false;
	}
	else {
		// Set object attributes to the form paramenters
		$this->temp_path = $file['tmp_name'];
		$this->filename	 = basename($file['name']);
		$this->type		 = $file['type'];
		$this->size	 	 = $file['size'];
		// Don't worry about saving anything to the database yet
		return true;
	}
}
/*
public function save() {
	parent::save();
}
*/
public function image_path() {
	return $this->upload_dir.DS.$this->filename;
}
public function size_as_text() {
	if($this->size < 1024) {
		return "{$tihs->size} bytes";
	}
	elseif($this->size < 1048576) {
		$size_kb = round($this->size/1024);
		return "{$size_kb} KB";
	}
	else {
		$size_mb = round($this->size/1048576, 1);
		return "{$size_mb} MB";
	}
}
public function save() {
	// A new record won't have an id yet
	if(isset($this->id)) {
	// really just to update the caption
		$this->update();
	}
	else {
	// make sure there are no errors (can't save if there are pre-existing errors)
	if(!empty($this->errors)) {
		return false;
	}
	// make sure the caption is not too long for the DB
	if(strlen($this->caption) >= 255) {
		$this->error[] = "The caption can only be 255 characters long.";
		return false;
	}
	// can't save without filename and temp location
	if(empty($this->filename) || empty($this->temp_path)) {
		$this->errors[] = "The file location was not availabe." . var_dump($this->temp_path) . var_dump($this->filename);;
		return false;
	}
	// determine the target_path
	$target_path = $this->upload_dir.DS.$this->filename;
	// make sure a file doesn't already exist in the target location
	if(file_exists($target_path)) {
		$this->errors[] = "The file {$this->filename} already exisits.";
		return false;
	}
	// attempt to move the file
	if(move_uploaded_file($this->temp_path, $target_path)) {
		// success
		// save a corresponding entry to the database
		if($this->create()) {
		// done with temp_path, the file isn't there anymore
			unset($this->temp_path);
			return true;
		}
	}
	else {
		// file was not moved
		$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
		return false;
	}
  }
}

public static function find_all() {
	return self::find_by_sql("SELECT * FROM " . self::$table);
}
public static function find_by_id($id=0) {
	global $database;
	$result = self::find_by_sql("SELECT * FROM  " . self::$table . " WHERE id=" . $database->clean_strings($id) . " LIMIT 1");
	return !empty($result) ? array_shift($result) : false; //array_shift pulls the first item out
}
public static function find_by_sql($sql="") {
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = self::instantiate($row);
	}
	return $object_array; // Must return an array!
}
public static function count_all() {
	global $database;
	$sql = "SELECT COUNT(*) FROM " .self::$table;
	$result = $database->query($sql);
	$row = $database(fetch_array($result));
	return array_shift($row);
}
private static function instantiate($record) {
	$object = new self; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}
private function has_attribute($attribute) {
	// get_object_vars returns an associative array with all attributes
	// (including private ones) as the keys and their current value as the value
	$object_vars = $this->attributes(); // not sql related so doesn't nee dot be sanitized
	// We don't care about the value, we just want to know if the key exists
	// Will return true or false
	return array_key_exists($attribute, $object_vars);
}
protected function attributes() {
	// return an array of attribute keys and their values
	$attributes = array();
	foreach(self::$db_fields as $field) {
		if(property_exists($this, $field)) {
			$attributes[$field] = $this->$field; // use dynamic variable
			}
		}
	return $attributes; 
}
protected function sanitized_attributes() {
	global $database;
	$clean_attributes = array();
	// sanitize the values before submitting
	// Note: does not alter the actual value of each attribute
	foreach($this->attributes() as $key => $value) {
		$clean_attributes[$key] = $database->clean_strings($value);
	}
	return $clean_attributes; 
}
// Made protected so that one cannot call this directly, must use save()
protected function create() {
	global $database;
	$attributes = $this->sanitized_attributes();
	$adjust = "ALTER TABLE users AUTO_INCREMENT = 1";
	$sql = "INSERT INTO " . self::$table . " (";
	$sql .= join(", ", array_keys($attributes));
	$sql .= ") VALUES ('";
	$sql .= join("', '", array_values($attributes));
	$sql .= "')";
	$noGaps = $database->query($adjust);
	if($database->query($sql)) {
		$this->id = $database->insert_id(); // Update the variable
		return true;
	}
	else {
		return false;
	}
}
public function destroy() {
	// first remove from the database entry
	if($this->delete()) {
		// then remove the file
		$target_path = $this->image_path();
		return unlink($target_path) ? true : false;
	}
	else {
		// database delete failed
		return false;
	}
}
protected function update() {
	global $database;
	$attributes = $this->sanitized_attributes();
	foreach($attributes as $key => $value) {
		$attribute_pairs[] = "{$key}='{$value}'";
	}
	$sql = "Update " . self::$table_name . " SET ";
	$sql .= join(", ", $attributes_pairs);
	$sql .= " WHERE id=" . $database->clean_strings($this->id);
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}
public function delete() {
	global $database;
	$sql = "DELETE FROM " . self::$table;
	$sql .= " WHERE id=" . $database->clean_strings($this->id);
	$sql .= " LIMIT 1";
	$database->query($sql);
	return ($database->affected_rows() == 1) ? true : false;
}
} // End class Images
?>

Open in new window

0
FairyBusiness
Asked:
FairyBusiness
  • 2
1 Solution
 
Aaron TomoskyTechnology ConsultantCommented:
Side note, you declare the function save() twice.
0
 
FairyBusinessAuthor Commented:
one of them is commented out
0
 
4e4enCommented:
Are you sure this is right?
$row = $database(fetch_array($result));

Open in new window



By looking at your code it looks like this is how it should be:
Line 144.
	$row = $database->fetch_array($result);

Open in new window

0
 
FairyBusinessAuthor Commented:
Thanks, that fixed that error!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now