View file includes/inc_om_upload.php

File size: 3.36Kb
<?php

if (!defined('SECURED'))
{
	die("Unknown Error!");
}

function om_upload($uploadedfile,$uploaddir,$max_filesize='',$accepted_extensions='',$image_res='') 
{
	$max_filesize=(int)$max_filesize;
	//$uploaddir = "./files";
	//$uploadedfile = $_POST['fail1'];
	if(empty($uploadedfile))
	{
		$error_msg = "Файл не был загружен.";
	}
	else
	{
		if (strlen($uploadedfile) > 0)
		{
			$array = explode('file=', $uploadedfile);
			$tmp_name = $array[0];
			$filebase64 = $array[1];
		}
		$tran = array(
					   "A"=>"А","a"=>"а","B"=>"Б","b"=>"б","V"=>"В",
					   "v"=>"в","G"=>"Г","g"=>"г","D"=>"Д","d"=>"д",
					   "E"=>"Е","e"=>"е","yo"=>"Ё","Zh"=>"Ж","zh"=>"ж",
					   "Z"=>"З","z"=>"з","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"=>"х",
					   "C"=>"Ц","c"=>"ц","Ch"=>"Ч","ch"=>"ч","Sh"=>"Ш",
					   "sh"=>"ш","Sch"=>"Щ","sch"=>"щ","''"=>"ъ",
					   "Y"=>"Ы","y"=>"ы","'"=>"ь","Ye"=>"Э",
					   "ye"=>"э","Yu"=>"Ю","yu"=>"ю","Ya"=>"Я","ya"=>"я",
					   "Yo"=>"ё"
					);
		$tran=array_flip($tran);
		$tmp_name=strtr($tmp_name,$tran);
		$tmp_name = ereg_replace("[^a-z0-9._-]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($tmp_name))));
		
		$tfl = pathinfo($tmp_name, PATHINFO_EXTENSION);
		if (count($accepted_extensions)>0 && !in_array($tfl, $accepted_extensions))
		{
			$error_msg = "В этой категорий к закачке разрешены только файлы форматов <b>".strtoupper(implode(', ',$accepted_extensions))."</b>.";
		}
		else
		{
			if ($max_filesize>0 && (strlen(base64_decode($filebase64)) >= 1024 * $max_filesize))
			{
				$error_msg = "Размер файла не должен превышать $max_filesize КБ (".($max_filesize * 1024)." байт).";
			}
			else
			{
				if (strlen($filebase64) > 0)
				{
					$fname = str_replace('.'.$tfl, null, $tmp_name);
					$n=1;						
					while(file_exists("$uploaddir/$fname$copy.$tfl")) 
					{
						$copy = "_copy" . $n;
						$n++;
					}
					
					$FileName = "$uploaddir/$fname$copy.$tfl";
					$Raw = "$fname$copy.$tfl";
					$filedata = base64_decode($filebase64);
					$fid = @fopen($FileName, "wb");

					if ($fid)
					{
						if (flock($fid, LOCK_EX))
						{
							fwrite($fid, $filedata);
							flock($fid, LOCK_UN);
						}
						fclose($fid);
					}
					if (file_exists($FileName) && filesize($FileName) == strlen($filedata))
					{
						list($image_width,$image_height) = @getimagesize($FileName);
						$max_image_width=$image_res[0];
						$max_image_height=$image_res[1];

						if(($max_image_width || $max_image_height) && (($image_width > $max_image_width) || ($image_height > $max_image_height))) 
						{
							$error_msg = "Размер изображения не должен превышать $max_image_width x $max_image_height пикселей.";
						}
						else 
						{
							$uploaded = true;
						}
					}
					else
					{
						$error_msg = 'Unknown Error!';
					}
				}
				else
				{
						$error_msg = 'Unknown Error!';
				}
			}
		}
	}
if(!empty($error_msg)) return array(false,$error_msg);
return array($uploaded,$Raw);
}
?>