Won't insert one field into db...help!?

admin

Administrator
Staff member
This is the code I am using for uploading a file. I want a file description associated with the uploaded file...but for some reason, it's not inserting the file description into the mySQL database. Any ideas why?

<?php

$my_max_file_size = "102400"; # in bytes
$image_max_width = "300";
$image_max_height = "300";
$the_path = ".";

$registered_types = array(

"text/htm" => ".htm",
"text/html" => ".html, .htm",
"text/plain" => ".txt",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"image/png" => ".png",
"application/vnd.ms-powerpoint" => ".ppt",
"application/pdf" => ".pdf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",

);

$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg", "text/htm","text/html","image/png", "application/msword", "application/vnd.ms-excel","application/vnd.ms-powerpoint", "application/pdf");

# --

function form($error=false) {

global $PHP_SELF,$my_max_file_size;

if ($error) print $error . "<br><br>";

print "\n<form ENCTYPE=\"multipart/form-data\" action=\"" . $PHP_SELF . "\" method=\"post\">";
print "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=http://www.phpbuilder.com/board/archive/index.php/\"" . $my_max_file_size . "\">";
print "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
print "\n<br><INPUT type=\"text\" name=filedescip SIZE=\"35\"><br>";
print "\n<P>Upload a file";
print "\n<BR>NOTE: Max file size is " . ($my_max_file_size / 1024) . "KB";
print "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";


print "\n<input type=\"submit\" Value=\"Upload\">";
print "\n</form>";

} # END form

# --

if (!ereg("^4",phpversion())) {
function in_array($needle,$haystack) { #
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 = "\n<b>Error:</b>\n<ul>";

if ($the_file == "none") { # do we even have a file?

$error .= "\n<li>You did not upload anything!</li>";

} else { # check if we are allowed to upload this file_type

if (!in_array($the_file_type,$allowed_types)) {
$error .= "\n<li>The file that you uploaded was of a type that is not allowed, you are only
allowed to upload files of the type:\n<ul>";
while ($type = current($allowed_types)) {
$error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
next($allowed_types);
}
$error .= "\n</ul>";
}

if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

$size = GetImageSize($the_file);
list($foo,$width,$bar,$height) = explode("\"",$size[3]);

if ($width > $image_max_width) {
$error .= "\n<li>Your image should be no wider than " . $image_max_width . " Pixels</li>";
}

if ($height > $image_max_height) {
$error .= "\n<li>Your image should be no higher than " . $image_max_height . " Pixels</li>";
}

}

if ($error) {
$error = $start_error . $error . "\n</ul>";
return $error;
} else {
return false;
}
}
} # END validate_upload



function upload($the_file) {

global $the_path,$the_file_name;

$error = validate_upload($the_file);
if ($error) {
form($error);
} else { # cool, we can continue

## begin database insert
$hostname = "host";
$username = "username";
$password = "pw";
$dbName = "dbname";

$linkID=@MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to database");
mysql_select_db("$dbName", $linkID) or die("Unable to select database");

$filename = "title.txt";
$fd = fopen ($filename, "r");
$title = fread ($fd, filesize ($filename));

$da = getdate();
$uploaddate = "$da[mday]/$da[mon]/$da[year]";

# make a query on a table in your database
list ($first, $ext) = explode (".", $the_file_name);


$query = "INSERT INTO filemanager (title, first, ext, date, filedesc) VALUES ('$title', '$first', '$ext', '$uploaddate', '$filedescip')";






$query_result_handle = mysql_query ($query)
or die ('The requested table or database does not exist');
## END DATABASE INSERT
if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
form("\n<b>Something barfed, check the path to and the permissions for the upload directory</b>");
} else {
## list_files();
form();
}
}
} # END upload

# --

############ Start page

print "\n<head>\n<title>Upload example</title>\n</head>\n<body>";

switch($task) {
case 'upload':
upload($the_file);
break;
default:
form();
}

print "\n</body>\n";

?>
 
Back
Top