dbid = DB::InsertQuery ( 'INSERT INTO files(`name`,`size`,`content`) ' . 'VALUES (?(name), ?(size), ?(content))', $this->fname, $this->size, $this->fcontent ); } public function __consruct_dbid ($id) { $f = DB::QueryResultAssoc( 'SELECT `name`,`size`,`content` FROM files WHERE id = ?(id)', $id ); if(is_null($f)) return FALSE; $this->dbid = $id; $this->fname = $f['name']; $this->size = $f['size']; $this->fcontent = $f['content']; } public function __construct_file ($fpath, $fname) { $fp = fopen($fpath, 'r'); $this->fname = $fname; $this->size = filesize($fpath); $this->fcontent = fread($fp, $this->size); fclose($fp); $this->save2db(); } public function __construct ($id_or_path, $fname = '') { if ( is_numeric($id_or_path) ) { $this->__consruct_dbid($id_or_path); } else { $this->__construct_file($id_or_path, $fname); } } public function __destruct () { if ( $this->tmpname !== NULL ) unlink($this->tmpname); } public function getFileID () { return $this->dbid; } // if $fpath is not specified DB file content will be dumped to // the temporary file that will be removed on object destructoin public function getFile ($fpath = NULL) { if ( $fpath == NULL ) { $this->tmpname = tempnam(sys_get_temp_dir(), $this->fname . '.'); $dumppath = $this->tmpname; } else { $dumppath = $fpath; } $fp = fopen($dumppath, 'w'); fwrite($fp, $this->fcontent); fclose($fp); return $dumppath; } public function rmFile () { $r = DB::UpdateQuery('DELETE FROM files WHERE id = ?(id)', $this->dbid); if(!is_null($r) && ($r != 0)) return TRUE; return FALSE; } public function updateFileContent ($newfpath) { $fp = fopen($newfpath, 'r'); $this->size = filesize($newfpath); $this->fcontent = fread($fp, $this->size); fclose($fp); $r = DB::UpdateQuery('UPDATE files SET `size` = ?(size),`content` = ?(cont) '. 'WHERE id = ?(id)', $this->size, $this->fcontent, $this->dbid); if(!is_null($r) && ($r != 0)) return TRUE; return FALSE; } } ?>