File: //home/temp/yarusvl.ru/api/File.php
<?php
require_once('Mgc.php');
class File extends Mgc {
private $allowed_extentions = array('doc', 'docx', 'xslx', 'xsl', 'pdf', 'png', 'jpg', 'jpeg', 'tif', 'gif', 'txt');
private $gregwar_image;
public function __construct() {
parent::__construct();
}
/*Загрузка изображения*/
public function upload($filename, $name, $original_dir = null) {
// Имя оригинального файла
var_dump($filename);
$name = preg_replace('~(.+)\.([0-9]*)x([0-9]*)(w)?\.([^\.\?]+)$~', '${1}.${5}', $name);
$name = $this->correct_filename($name);
$uploaded_file = $new_name = pathinfo($name, PATHINFO_BASENAME);
$base = pathinfo($uploaded_file, PATHINFO_FILENAME);
$ext = pathinfo($uploaded_file, PATHINFO_EXTENSION);
if (!$original_dir) {
$original_dir = $this->config->original_images_dir;
}
if(in_array(strtolower($ext), $this->allowed_extentions)) {
while(file_exists($this->config->root_dir.$original_dir.$new_name)) {
$new_base = pathinfo($new_name, PATHINFO_FILENAME);
if(preg_match('/_([0-9]+)$/', $new_base, $parts)) {
$new_name = $base.'_'.($parts[1]+1).'.'.$ext;
} else {
$new_name = $base.'_1.'.$ext;
}
}
if(move_uploaded_file($filename, $this->config->root_dir.$original_dir.$new_name)) {
return $original_dir.$new_name;
}
}
return false;
}
private function files_identical($fn1, $fn2) {
$buffer_len = 1024;
if(!$fp1 = fopen(dirname(dirname(__FILE__)).'/'.$fn1, 'rb')) {
return FALSE;
}
if(!$fp2 = fopen($fn2, 'rb')) {
fclose($fp1);
return FALSE;
}
$same = TRUE;
while (!feof($fp1) and !feof($fp2)) {
if(fread($fp1, $buffer_len) !== fread($fp2, $buffer_len)) {
$same = FALSE;
break;
}
}
if(feof($fp1) !== feof($fp2)) {
$same = FALSE;
}
fclose($fp1);
fclose($fp2);
return $same;
}
/*Транслит названия изображения*/
public function correct_filename($filename) {
$ru = explode('-', "А-а-Б-б-В-в-Ґ-ґ-Г-г-Д-д-Е-е-Ё-ё-Є-є-Ж-ж-З-з-И-и-І-і-Ї-ї-Й-й-К-к-Л-л-М-м-Н-н-О-о-П-п-Р-р-С-с-Т-т-У-у-Ф-ф-Х-х-Ц-ц-Ч-ч-Ш-ш-Щ-щ-Ъ-ъ-Ы-ы-Ь-ь-Э-э-Ю-ю-Я-я");
$en = explode('-', "A-a-B-b-V-v-G-g-G-g-D-d-E-e-E-e-E-e-ZH-zh-Z-z-I-i-I-i-I-i-J-j-K-k-L-l-M-m-N-n-O-o-P-p-R-r-S-s-T-t-U-u-F-f-H-h-TS-ts-CH-ch-SH-sh-SCH-sch---Y-y---E-e-YU-yu-YA-ya");
$res = str_replace($ru, $en, $filename);
$res = preg_replace("/[\s]+/ui", '-', $res);
$res = preg_replace("/[^a-zA-Z0-9\.\-\_]+/ui", '', $res);
$res = strtolower($res);
return $res;
}
}