Location: PHPKode > projects > Dev's CMS > devscms/includes/class.files.php
<?php

system::includeLib('filehandling');

class files
{
	/**
	 * @var mysqliConnection
	 */
	private $db;
	private $tableName;
	
	public function __construct(&$db)
	{
		$this->db = &$db;
		$this->tableName = config::get('files', 'tableName');
		
		$db->checkTable($this->tableName, "
			`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
			`filename` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
			`filesize` int(10) unsigned NOT NULL,
			`hash` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
			`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
			`description` text COLLATE utf8_unicode_ci NOT NULL,
			`added` datetime NOT NULL,
			`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (`id`)
		");
		
		if (!is_dir(config::get('files', 'path'))) {
			mkdir(config::get('files', 'path'));
		}
	}
	
	public function edit($id, $title, $description)
	{
		$this->db->update($this->tableName,
			array('title' => $title,
				'description' => $description),
			'id = '.$this->db->quote($id));
	}
	
	public function getAll($cols = '*')
	{
		$tableName = $this->tableName;
		
		$sqlQuery = "SELECT $cols,UNIX_TIMESTAMP(added) AS added_unix,UNIX_TIMESTAMP(updated) as updated_unix FROM $tableName ORDER BY updated DESC";
		return $this->db->getRows($sqlQuery);
	}
	
	public function getByHash($hash)
	{
		return $this->db->getRow($this->tableName, '*,UNIX_TIMESTAMP(added) AS added_unix,UNIX_TIMESTAMP(updated) AS updated_unix', 'hash', $hash);
	}
	
	public function getById($id)
	{
		return $this->db->getRow($this->tableName, '*,UNIX_TIMESTAMP(added) AS added_unix,UNIX_TIMESTAMP(updated) AS updated_unix', 'id', $id);
	}
	
	public function getInfo($file)
	{
		if (is_null($file)) {
			die('File is null');
		}
		
		$icons = array('pdf' => 'admin/' . ROOT_DIR . 'images/icon_pdf_small.gif');
		$units = array('kB', 'MB');
		
		$fileInfo['title'] = $file['title'];
		$fileInfo['description'] = nl2br($file['description']);
		
		$fileInfo['filename'] = config::get('files', 'path') . $file['filename'];
		$fileInfo['extension'] = strtolower(filehandling::getExtension($file['filename']));
		
		if (isset($icons[$fileInfo['extension']])) {
			$fileInfo['icon'] = $icons[$fileInfo['extension']];
		} else {
			$fileInfo['icon'] = 'admin/' . ROOT_DIR . 'images/icon_other.gif';
		}
		
		for ($i = 0; $i < count($units); $i++) {
			$file['filesize'] /= 1024;
			
			if ($file['filesize'] < 1000) {
				$fileInfo['size'] = number_format($file['filesize'], 1) . ' ' . $units[$i];
				break;
			}
		}
		
		setlocale(LC_ALL, config::get('system', 'localeLong'), config::get('system', 'localeShort'));
		$fileInfo['added'] = strftime('%d %B %Y, %H:%M', $file['added_unix']);
		$fileInfo['updated'] = strftime('%d %B %Y, %H:%M', $file['updated_unix']);
		
		return $fileInfo;
	}
	
	public function upload($filename, $filesize, $title, $description)
	{
		$hash = md5_file(config::get('files', 'path') . $filename);
		
		if (empty($title)) {
			$title = $filename;
		}
		
		$this->db->insert($this->tableName,
			array('filename' => $filename,
				'filesize' => $filesize,
				'hash' => $hash,
				'title' => $title,
				'description' => $description,
				'added' => 'FROM_UNIXTIME(' . time() . ')'));
		return $this->db->lastInsertedId();
	}
	
	public function uploadNew($id, $filename, $filesize)
	{
		$hash = md5_file(config::get('files', 'path') . $filename);
		
		$this->db->update($this->tableName,
			array('filename' => $filename,
				'filesize' => $filesize,
				'hash' => $hash),
			'id = '.$this->db->quote($id));
	}
	
	public function remove($id)
	{
		$file = $this->getById($id);
		unlink(config::get('files', 'path') . $file['filename']);
		
		$this->db->delete($this->tableName, 'id = '.$this->db->quote($id));
	}
}
Return current item: Dev's CMS