upload file form with textarea for description
Posted: Mon Feb 16, 2004 6:13 pm
Hi guys,
I am just having some trouble with a form which allows users to upload a file and type in a description to the file.
When the user clicks submit, I want the following to happen.
1. Validates fields (ie has the user browsed for a file and clicked okay and also entered a description into a textarea - if not, echo an error message)
2. invoke function to check if file is of accepted type, size etc (whatever - this function works)
3. upload the file and display a list of the uploaded files.
Code below.
Any thoughts?
$my_max_file_size = "102400"; # in bytes
$the_path = "pdfs";
$registered_types = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"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",
"application/pdf" => ".pdf"
);
$allowed_types = array("application/pdf");
function validate_upload($the_file) {
global $my_max_file_size, $allowed_types, $the_file_type, $registered_types;
// $start_error = "<b class=\"bodyText\">Error:</b>\n<ul>";
if (!in_array($the_file_type, $allowed_types)) {
$error .= "\n<div align=\"center\"><li class=\"bodyTextRed\">The file that you uploaded was of a ".
"type that is not allowed,<br>you are only allowed to upload files of the type:\n";
while ($type = current($allowed_types)) {
$error .= "<span class=\"bodyTextRed\"><b>" . $registered_types[$type] . " (" . $type . ")</b></span>";
next($allowed_types);
}
$error .= "\n</div>";
}
if ($error) {
$error = $start_error . $error . "\n</ul>";
return $error;
} else {
return false;
}
}
function upload($the_file, $the_description) {
global $the_path, $the_file_name;
if( $the_file == "" || $the_description = "") {
echo "<p class=\"bodyTextRed\" align=\"center\">You did not select a file or enter a description.</p>";
}
else {
$error = validate_upload($the_file);
if ($error) {
echo "<p class=\"bodyTextRed\">$error</p>";
} else {
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
echo "\n<p class=\"bodyTextRed\"><b>Upload error. Check your file and path permissions.</b></p>";
} else {
echo "\n<p class=\"bodyTextRed\" align=\"center\">File successfully uploaded</p>";
}
}
}
} # END upload
// if( $action = 'upload' ) {
if ($submit) {
// echo $the_file, $the_description;
upload( $the_file, $the_description);
}
Sorry if the code isnt formatted properly.
TIA
I am just having some trouble with a form which allows users to upload a file and type in a description to the file.
When the user clicks submit, I want the following to happen.
1. Validates fields (ie has the user browsed for a file and clicked okay and also entered a description into a textarea - if not, echo an error message)
2. invoke function to check if file is of accepted type, size etc (whatever - this function works)
3. upload the file and display a list of the uploaded files.
Code below.
Any thoughts?
$my_max_file_size = "102400"; # in bytes
$the_path = "pdfs";
$registered_types = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"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",
"application/pdf" => ".pdf"
);
$allowed_types = array("application/pdf");
function validate_upload($the_file) {
global $my_max_file_size, $allowed_types, $the_file_type, $registered_types;
// $start_error = "<b class=\"bodyText\">Error:</b>\n<ul>";
if (!in_array($the_file_type, $allowed_types)) {
$error .= "\n<div align=\"center\"><li class=\"bodyTextRed\">The file that you uploaded was of a ".
"type that is not allowed,<br>you are only allowed to upload files of the type:\n";
while ($type = current($allowed_types)) {
$error .= "<span class=\"bodyTextRed\"><b>" . $registered_types[$type] . " (" . $type . ")</b></span>";
next($allowed_types);
}
$error .= "\n</div>";
}
if ($error) {
$error = $start_error . $error . "\n</ul>";
return $error;
} else {
return false;
}
}
function upload($the_file, $the_description) {
global $the_path, $the_file_name;
if( $the_file == "" || $the_description = "") {
echo "<p class=\"bodyTextRed\" align=\"center\">You did not select a file or enter a description.</p>";
}
else {
$error = validate_upload($the_file);
if ($error) {
echo "<p class=\"bodyTextRed\">$error</p>";
} else {
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
echo "\n<p class=\"bodyTextRed\"><b>Upload error. Check your file and path permissions.</b></p>";
} else {
echo "\n<p class=\"bodyTextRed\" align=\"center\">File successfully uploaded</p>";
}
}
}
} # END upload
// if( $action = 'upload' ) {
if ($submit) {
// echo $the_file, $the_description;
upload( $the_file, $the_description);
}
Sorry if the code isnt formatted properly.
TIA