2011-03-13 6 views
2

저는 PHP 비디오 북/자습서를 사용하여 photo_gallery를 만드는 방법을 배우고 있습니다. 사진을 업로드 할 수 있었지만 MySQL 데이터베이스에서는 사진의 크기가 등록되지 않았습니다. 그것은 단지 0을 말합니다 (이미지 참조)MYSQL 파일 크기가 표시되지 않습니다

강사의 Mysql은 파일의 크기를 제공합니다.

파일 크기가 SQL에 표시되지 않는 이유는 무엇입니까?

MySQL

UPDATE ...이 사진 클래스입니다.

<?php 

require_once(LIB_PATH.DS.'database.php'); 

class Photograph extends DatabaseObject { 

protected static $table_name="photographs"; 
protected static $db_fields=array('id', 'filename', 'type', '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(
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 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 parameters. 
$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() { 
// 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 available."; 
return false; 
} 

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

//Make sure a file doesn't already exist 

if(file_exists($target_path)) { 
$this->errors[] = "The file {$this->filename} already exists."; 
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()) { 
//we are done with temp_path, the file isn't there anymore 
unset($this->temp_path); 
return true; 
} 
} else { 
//failure 
$this->errors[] = "The file upload failed, possibly due to incorrect permissions 
on the upload folder."; 
return false; 
     } 
    } 
} 

//common database methods 

public static function find_all(){ 
return self::find_by_sql("SELECT * FROM ".self::$table_name); 

} 

public static function find_by_id($id=0) { 
global $database; 
$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id={$id} LIMIT 1"); 
return !empty($result_array) ? array_shift($result_array) : false; 
} 

public static function find_by_sql($sql=""){ 
global $database; 
$result_set = $database->query($sql); 
$object_array = array(); 
while ($row = $database->fetch_array($result_set)) { 
$object_array[] = self::instantiate($row); 
} 
return $object_array; 
} 




private static function instantiate($record){ 

$object = new self; 
//$object->id = $record['id']; 
//$object->username = $record['username']; 
//$object->password = $record['password']; 
//$object->first_name = $record['first_name']; 
//$object->last_name = $record['last_name']; 

foreach($record as $attribute=>$value) { 
if($object->has_attribute($attribute)) { 
$object->$attribute = $value; 
} 
} 
return $object; 
} 

private function has_attribute($attribute) { 
$object_vars = $this->attributes(); 
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; 
} 
} 
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->escape_value($value); 
} 
    return $clean_attributes; 
} 

//replaced with a custom save() 
//public function save() { 
//return isset($this->id) ? $this->update() : $this->create(); 
//} 

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; 
} 
} 

public function update() { 
global $database; 
$attributes = $this->sanitized_attributes(); 
$attribute_pairs = array(); 
foreach($attributes as $key => $value) { 
    $attribute_pairs[] = "{$key}='{$value}'"; 
} 
$sql = "UPDATE ".self::$table_name." SET "; 
$sql .= join(", ", $attribute_pairs); 
$sql .= " WHERE id=". $database->escape_value($this->id); 
$database->query($sql); 
return($database->affected_rows() == 1) ? true : false; 
} 

public function delete() { 
global $database; 
$sql = "DELETE FROM ".self::$table_name." "; 
$sql .= "WHERE id=". $database->escape_value($this->id); 
$sql .= " LIMIT 1"; 
$database->query($sql); 
return($database->affected_rows() == 1) ? true : false; 

} 

} 

?>

+1

어떻게 데이터를 업로드하고 주입합니까? – kjy112

+0

어떻게 값을 삽입하나요? – bensiu

+0

사진 클래스의'photo-> attach' 메소드에 코드 조각을 보여줄 수 있습니까? 그리고 마이클 당신은 나의 이전 대답을 수락하지 않았습니다 –

답변

3

나는 당신이 다음 $db_fields 및 내 property_exists이를 할당 여부를 확인하여 attributes() 기능을 보면 당신이 당신의 $db_fields

protected static $db_fields=array('id', 'filename', 'type', 'size', 'caption'); 

의 크기를 넣어하는 것을 잊지 생각 값을 $attributes 배열로 그리고 '크기'가 검사 할 배열 내에 없기 때문에 기본적으로 필터링됩니다.

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; 
     } 
    } 
    return $attributes; 
} 
관련 문제