Esempio n. 1
0
 public function uploadPicture()
 {
     if (isset($_FILES['book_picture']) && $_FILES['book_picture']['tmp_name']) {
         $file = $_FILES['book_picture'];
     } else {
         return false;
     }
     $size = getimagesize($file['tmp_name']);
     if ($size[0] > 200 && $size[1] > 200) {
         $this->_errors[] = 'Не верный размер загружаемого изображения. Размеры картинки должны быть ограничены 200x200px';
         return false;
     }
     $validTypes = array("image/jpg" => 'jpg', "image/jpeg" => 'jpeg', "image/gif" => 'gif', "image/png" => 'png');
     if (in_array($size['mime'], array_keys($validTypes))) {
         $extension = $validTypes[$size['mime']];
     } else {
         $this->_errors[] = 'Не верный формат загружаемого изображения. Разрешено загружать форматы jpg, jpeg, gif, png';
         return false;
     }
     $config = Configs::getBlock('upload');
     $uploadDir = $config->dir;
     $fileName = md5(time() . $file['name']) . "." . $extension;
     $uploadFile = $uploadDir . $fileName;
     if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
         if ($this->picture && file_exists($uploadDir . $this->picture)) {
             unlink($uploadDir . $this->picture);
         }
         $this->picture = $fileName;
         return true;
     } else {
         $this->picture = '';
         $this->_errors[] = 'Не удалось загрузить картинку.';
         return false;
     }
 }
Esempio n. 2
0
 private function __construct()
 {
     self::$_dbParams = Configs::getBlock('database');
     if (!self::$_dbParams) {
         throw new Exception('Error: Empty database connection params.');
     }
     $dbParams = self::$_dbParams;
     $connection = mysqli_connect($dbParams->host, $dbParams->user, $dbParams->password);
     if (!$connection) {
         throw new Exception('Error: Unable to connect to MySQL.');
     }
     $db = mysqli_select_db($connection, $dbParams->database);
     if (!$db) {
         throw new Exception('Error: Unable to connect to MySQL.');
     }
     mysqli_set_charset($connection, "utf8");
     self::$_connection = $connection;
     self::$_db = $db;
 }