Codice PHP:
$my_max_file_size = "307200"; # in bytes
$image_max_width = "400";
$image_max_height = "500";
$registered_types = array(
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"image/png" => ".png",
"image/x-png" => ".png",
"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/png","image/x-png","image/pjpeg","image/jpeg","image/bmp","application/x-shockwave-flash");
//end global variables
function dbconnect() {
mysql_connect('');
@mysql_select_db("") or die ("Unable to select database");
}
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 . "
";
echo "\n<form ENCTYPE=\"multipart/form-data\" action=\"upload66.php\" method=\"post\">";
echo "\n
<CENTER><h2>[b]Upload Files[/b]</center></h2>";
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
[b]File1[/b]: <INPUT NAME=\"thefile\" TYPE=\"file\" SIZE=\"35\">
";
echo "\n
dimensione massima: " . ($my_max_file_size / 1024) . "KB - ".$image_max_width."x".$image_max_height." pixel";
echo "\n
<input type=\"submit\" Value=\"Invia\" name=\"send\">";
echo "\n</form>";
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[*]Nessun file selezionato";
} else {
if (!in_array($thefile_type, $allowed_types)) {
$error .= "\n[*]Il file non appartiene a queste tipologie:
\n<ul>";
while ($type = current($allowed_types)) {
$error .= "\n[*]" . $registered_types[$type] . " (" . $type . ")";
next($allowed_types);
}
$error .= "\n[/list]";
}
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[*]La larghezza dell'immagine non deve superare " . $image_max_width . " pixel";
}
$g_height=$height;
if ($height > $image_max_height) {
$error .= "\n[*]L'altezza dell'immagine non deve superare " . $image_max_height . " pixels";
}
}
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[/list]";
return $error;
} else {
return false;
}
}
} # END validate_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 db VALUES ('NULL', '$data','$thefile_name','$thefile_size','$thefile_type', '$g_width', '$g_height')");
fclose($fp);
if (!$result) { echo mysql_errno(). ": ".mysql_error(). "
"; exit();}
else {
Header("Location: uploadprova.php");
}
} else {
echo"Il file selezionato non è valido, probabilmente è vuoto";
fclose($fp);
}
}
} # END upload
dbconnect();
switch($task) {
case 'upload':
upload($thefile);
break;
default:
form($error);
}