<?php
//Based on (http://www.phpbuilder.com/columns/bealers20000904.php3) "File uploads made easy" by Darren Beale
//changes by Zubin for MMkit.com - [email protected]
// global variables
$my_max_file_size = "307200"; # in bytes
$image_max_width = "400";
$image_max_height = "500";
$registered_types = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"application/x-shockwave-flash" => ".swf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",
"application/octet-stream" => ".exe, .fla"
); # these are only a few examples, you can find many more!
$allowed_types = array("image/gif","image/pjpeg","image/jpeg","application/x-shockwave-flash");
//end global variables
function dbconnect() {
mysql_connect("localhost", "root", "");
@mysql_select_db("db_immagini") or die ("Unable to select database");
}
function delete($id) {
mysql_query("delete from mmkit_images WHERE id=$id");
Header("Location: upload.php");
}
function mod($id) {
global $PHP_SELF, $my_max_file_size, $image_max_width, $image_max_height;
echo"<html><head><title>Uploading Files</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>
<body bgcolor=\"#EAEAEA\">";
echo "\n<form ENCTYPE=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
echo "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
echo "\n<P><CENTER><h2><b>Modifica immagine id=$id</b></center></h2>";
echo "\n<br><b>File</b>: <INPUT NAME=\"thefile\" TYPE=\"file\" SIZE=\"35\"><br>";
echo "\n<BR>dimensione massima: " . ($my_max_file_size / 1024) . "KB - ".$image_max_width."x".$image_max_height." pixel";
echo "<input type=\"hidden\" name=\"id\" value=\"$id\"><input type=\"hidden\" name=\"task\" value=\"change\">";
echo "\n<br><input type=\"submit\" Value=\"Invia\">";
echo "\n</form>";
echo"</body></html>";
} # END form
function form($error=false) {
global $PHP_SELF, $my_max_file_size, $image_max_width, $image_max_height;
echo"<html><head><title>Uploading Files</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>
<body bgcolor=\"#EAEAEA\">";
if ($error) print $error . "<br><br>";
echo "\n<form ENCTYPE=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
echo "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
echo "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
echo "\n<P><CENTER><h2><b>Upload Files</b></center></h2>";
echo "\n<br><b>File</b>: <INPUT NAME=\"thefile\" TYPE=\"file\" SIZE=\"35\"><br>";
echo "\n<BR>dimensione massima: " . ($my_max_file_size / 1024) . "KB - ".$image_max_width."x".$image_max_height." pixel";
echo "\n<br><input type=\"submit\" Value=\"Invia\">";
echo "\n</form>";
list_files();
echo"</body></html>";
} # END form
function validate_upload($thefile) {
global $my_max_file_size, $image_max_width, $image_max_height, $allowed_types, $thefile_type, $registered_types, $g_height, $g_width, $thefile_name;
$start_error = "\n<b>Si sono verificati i seguenti errori:</b>\n<ul>";
if ($thefile == "none") {
$error .= "\n<li>Nessun file selezionato</li>";
} else {
if (!in_array($thefile_type, $allowed_types)) {
$error .= "\n<li>Il file non appartiene a queste tipologie:
<br>\n<ul>";
while ($type = current($allowed_types)) {
$error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
next($allowed_types);
}
$error .= "\n</ul>";
}
if (ereg("image", $thefile_type) && in_array($thefile_type, $allowed_types)) {
$size = GetImageSize($thefile);
list($foo,$width,$bar,$height) = explode("\"",$size[3]);
$g_width=$width;
if ($width > $image_max_width) {
$error .= "\n<li>La larghezza dell'immagine non deve superare " . $image_max_width . " pixel</li>";
}
$g_height=$height;
if ($height > $image_max_height) {
$error .= "\n<li>L'altezza dell'immagine non deve superare " . $image_max_height . " pixels</li>";
}
}
if (ereg("flash", $thefile_type) && in_array($thefile_type, $allowed_types)) {
$size = GetImageSize($thefile);
list($foo,$width,$bar,$height) = explode("\"",$size[3]);
$g_width=$width;
$g_height=$height;
if (($width > $image_max_width) || ($height > $image_max_height)) {
if ($width >= $height) {
$g_width=$image_max_width;
$g_height=intval($height*$image_max_width/$width);
}
else {
$g_height=$image_max_height;
$g_width=intval($width*$image_max_height/$height);
}
}
}
if ($error) {
$error = $start_error . $error . "\n</ul>";
return $error;
} else {
return false;
}
}
} # END validate_upload
function list_files() {
$result=mysql_query("select id, filename, filetype, filewidth, fileheight from mmkit_images");
if (mysql_num_rows($result) > 0) {
echo"<table width=\"98%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\" bordercolor=\"#000000\"><tr bgcolor=\"#999999\"><td>Files presenti nel database:</td></tr>";
while(list($id, $filename, $filetype, $width, $height)=mysql_fetch_row($result)) {
echo"<tr bgcolor=\"#cccccc\"><td><a href=\"upload.php?task=delete&id=$id\">Delete</a> - <a href=\"upload.php?task=mod&id=$id\">Modify</a></td></tr>";
if (ereg("flash", $filetype)) {
echo"<tr bgcolor=\"#FFFFFF\"><td><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" width=\"$width\" height=\"$height\"><param name=movie value=\"getdata.php?id=$id\"><param name=quality value=high><embed src=\"getdata.php?id=$id\" quality=high pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"$width\" height=\"$height\"></embed></object></td>
</tr>";
} else {
echo"<tr bgcolor=\"#FFFFFF\"><td><img src=\"getdata.php?id=$id\" width=\"$width\" height=\"$height\"></td>
</tr>";
}
}
echo"</table>";
}
else echo"<br>Il database è vuoto.";
}//end list_files
function change($thefile, $id) {
global $thefile_name, $thefile_type, $thefile_size, $g_width, $g_height;
$error = validate_upload($thefile);
if ($error) {
form($error);
} else {
$fp=@fopen($thefile, "r");
if ($fp) {
$data = addslashes(fread($fp, filesize($thefile)));
$result=MYSQL_QUERY("UPDATE mmkit_images SET bin_data='$data', filename='$thefile_name', filesize='$thefile_size', filetype='$thefile_type', filewidth='$g_width', fileheight='$g_height' WHERE id=$id");
fclose($fp);
if (!$result) { echo mysql_errno(). ": ".mysql_error(). "<br>"; exit();}
else Header("Location: upload.php");
} else {
echo"Il file selezionato non è valido, probabilmente è vuoto.<br><a href=\"javascript:history.back()\">Clicca qui</a> per tornare indietro.";
fclose($fp);
}
}
} # END upload
function upload($thefile) {
global $thefile_name, $thefile_type, $thefile_size, $g_width, $g_height;
$error = validate_upload($thefile);
if ($error) {
form($error);
} else {
$fp=@fopen($thefile, "r");
if ($fp) {
$data = addslashes(fread($fp, filesize($thefile)));
$result=MYSQL_QUERY("INSERT INTO mmkit_images VALUES ('NULL', '$data','$thefile_name','$thefile_size','$thefile_type', '$g_width', '$g_height')");
fclose($fp);
if (!$result) { echo mysql_errno(). ": ".mysql_error(). "<br>"; exit();}
else Header("Location: upload.php");
} else {
echo"Il file selezionato non è valido, probabilmente è vuoto.<br><a href=\"javascript:history.back()\">Clicca qui</a> per tornare indietro.";
fclose($fp);
}
}
} # END upload
dbconnect();
switch($task) {
case 'upload':
upload($thefile);
break;
case 'delete':
delete($id);
break;
case 'mod':
mod($id);
break;
case 'change':
change($thefile, $id);
break;
default:
form($error);
}
?>