<?
echo '<?xml version="1.0" encoding="iso-8859-2"?>';
$allowed_extensions = array('jpg', 'jpeg');
$download_path = 'images/';
$max_file_size = 5242880;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" />
<title>
Upload.com
</title>
<style>
.error { color: red }
a { color: gray; text-decoration: none }
a:visited { color: gray; text-decoration: none }
a:hover { color: gray; text-decoration: underline }
</style>
</head>
<body>
<div align="center">
<a href="index.php">File list</a>
<form enctype="multipart/form-data" action="upload_.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value=<?=$max_file_size?>/>
<input name="file" type="file" size="40"/><br />
<input type="submit" value="Send Your File"/>
</form>
<?
$tmp_file = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$extension = explode(".", $name);
$ext = end($extension);
if(is_uploaded_file($tmp_file)):
if(!in_array($ext, $allowed_extensions)):
echo '<span class="error">Not allowed extension.</span>';
return true;
endif;
$newname = createName($ext);
move_uploaded_file($tmp_file, $download_path.''.$newname);
echo '<strong><a href="download.php?file='.$newname.'">'.$newname.'</a></strong> (<strong>'.size($size).'</strong>) has been succesfully uploaded.';
endif;
function createName($extension)
{
$length = 6;
$pattern = 'ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
$max = strlen($pattern) - 1;
$random = '';
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
$random .= $pattern[mt_rand(0, $max)];
return $random.'.'.$extension;
}
function size($size)
{
$size = $size;
$s = 'b';
if($size >= 1073741824):
$size = $size / 1024 / 1024 / 1024;
$s = 'gb';
elseif($size >= 1048576):
$size = $size / 1024 / 1024;
$s = 'mb';
elseif($size >= 1024):
$size = $size / 1024;
$s = 'kb';
endif;
return round($size, 2).' '.$s;
}
?>
</div>
</body>
</html>