".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/postscript" => ".ps, .eps",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc (etc)",
"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 (etc)",
"image/x-ps" => ".ps"
); # these are only a few examples, you can find many more!
$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg","application/postscript");
# --
function form($error=false) {
global $PHP_SELF,$my_max_file_size;
if ($error) print $error . "
";
print "\n
";
} # END form
# --
if (!ereg("^4",phpversion())) {
function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
for ($i=0; $i < count($haystack); $i++) {
if ($haystack[$i] == $needle) {
return true;
}
}
}
}
# --
function validate_upload($the_file) {
global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;
$start_error = "\nError:\n";
if ($the_file == "none") { # do we even have a file?
$error .= "\n- You did not upload anything!
";
} else { # check if we are allowed to upload this file_type
if (!in_array($the_file_type,$allowed_types)) {
$error .= "\n- The file that you uploaded was of a type that is not allowed, you are only
allowed to upload files of the type:\n
";
while ($type = current($allowed_types)) {
$error .= "\n- " . $registered_types[$type] . " (" . $type . ")
";
next($allowed_types);
}
$error .= "\n
";
}
if ($error) {
$error = $start_error . $error . "\n
";
return $error;
} else {
return false;
}
}
} # END validate_upload
# --
function list_files() {
global $the_path;
$handle = dir($the_path);
print "\nUploaded files:
";
while ($file = $handle->read()) {
if (($file != ".") && ($file != "..")) {
print "\n" . $file . "
";
}
}
print "
";
}
# --
function upload($the_file) {
global $the_path,$the_file_name;
$error = validate_upload($the_file);
if ($error) {
form($error);
} else { # cool, we can continue
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
form("\nSomething barfed, check the path to and the permissions for the upload directory");
} else {
list_files();
form();
}
}
} # END upload
# --
############ Start page
print "\n\nUpload example\n\n";
switch($task) {
case 'upload':
upload($the_file);
break;
default:
form();
}
print "\n\n";
?>