Hi..
I have a form with some text fields and a file field. When I try uploading files, I get my "No File Selected" for some files and other file uploads work fine. All files that I'm testing with have been less than 100k.
Can you please take a look at my code and let me know what I can improve/change.
Thanks.
<?php
include("includes/main.php
");
mysql_connect ($db_hostname, $db_username, $db_password) or die ("Error locating database. Please try again later.");
mysql_select_db($db_name) or die ("Unable to connect to the database. Please try again later.");
$var_errornum = 0;
$var_errormsg = "";
$maxfilesize = 10485760;
if(isset($_REQUEST["txt_fi
rsttime"])
== "no"){
$var_fname = $_REQUEST["txt_fname"];
$var_lname = $_REQUEST["txt_lname"];
$var_company = $_REQUEST["txt_company"];
$var_address = $_REQUEST["txt_address"];
$var_city = $_REQUEST["txt_city"];
$var_province = $_REQUEST["txt_province"];
$var_country = $_REQUEST["txt_country"];
$var_postal = strtoupper($_REQUEST["txt_
postal"]);
$var_phone = $_REQUEST["txt_phone"];
$var_ext = $_REQUEST["txt_ext"];
$var_email = $_REQUEST["txt_email"];
$var_type = $_REQUEST["sel_type"];
$var_material = $_REQUEST["txt_material"];
$var_resolution = $_REQUEST["sel_resolution"
];
$var_color = $_REQUEST["sel_color"];
$var_comments = $_REQUEST["txt_comments"];
$var_datetime = date("Y-m-d H:i:s");
if(strlen($var_fname) == 0){
$var_errornum = 1;
$var_errormsg = "First name is required. Please enter your first name.";
} elseif(strlen($var_lname) == 0){
$var_errornum = 1;
$var_errormsg = "Last name is required. Please enter your last name.";
} elseif(strlen($var_address
) == 0){
$var_errornum = 1;
$var_errormsg = "Address is required. Please enter your address.";
} elseif(strlen($var_city) == 0){
$var_errornum = 1;
$var_errormsg = "City is required. Please enter your city.";
} elseif(strlen($var_provinc
e) == 0){
$var_errornum = 1;
$var_errormsg = "Province is required. Please enter your province.";
} elseif(strlen($var_country
) == 0){
$var_errornum = 1;
$var_errormsg = "Country is required. Please enter your country.";
} elseif(strlen($var_postal)
== 0){
$var_errornum = 1;
$var_errormsg = "Postal code is required. Please enter your postal code.";
} elseif(strlen($var_phone) == 0){
$var_errornum = 1;
$var_errormsg = "Phone number is required. Please enter your phone number.";
} elseif(strlen($var_email) == 0){
$var_errornum = 1;
$var_errormsg = "Email address is required. Please enter your valid email address.";
} elseif(!eregi('^[a-z0-9._-
]+@[a-z0-9
._-]+\.([a
-z]{2,4})$
', $var_email)){
$var_errornum = 1;
$var_errormsg = "The email address you entered contains invalid character(s) and/or wrong format.";
} elseif (!is_uploaded_file($HTTP_P
OST_FILES[
'myfile'][
'tmp_name'
])) {
$var_errornum = 1;
$var_errormsg = "NO FILE WAS SELECTED FOR UPLOAD!<br>Please click on <i>Browse</i> and select your CAD document from your hard drive.";
unlink($HTTP_POST_FILES['m
yfile']['t
mp_name'])
;
} elseif ($HTTP_POST_FILES['myfile'
]['size'] > $maxfilesize) {
$var_errornum = 1;
$var_errormsg = "File is too large. Try compressing (zipping) your file and then uploading it.";
unlink($HTTP_POST_FILES['m
yfile']['t
mp_name'])
;
}
if($var_errornum == 0){
$result = mysql_query("INSERT INTO tbl_quotes (quote_fname, quote_lname, quote_company, quote_address, quote_city, quote_province, quote_country, quote_postal, quote_phone, quote_ext, quote_email, quote_type, quote_material, quote_resolution, quote_color, quote_comments, quote_datetime) VALUES ('$var_fname', '$var_lname', '$var_company', '$var_address', '$var_city', '$var_province', '$var_country', '$var_postal', '$var_phone', '$var_ext', '$var_email', '$var_type', '$var_material', '$var_resolution', '$var_color', '$var_comments', '$var_datetime')") OR die("There has been an expected error. Please try again.".mysql_error());
$var_autonumber = mysql_insert_id();
$uploaddir = "content/quotes/";
$uploadfile = $uploaddir . basename($_FILES['myfile']
['name']);
if (move_uploaded_file($_FILE
S['myfile'
]['tmp_nam
e'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
}
}
else
{
$var_fname = "";
$var_lname = "";
$var_company = "";
$var_address = "";
$var_city = "";
$var_province = "";
$var_country = "";
$var_postal = "";
$var_phone = "";
$var_ext = "";
$var_email = "";
$var_type = 0;
$var_material = "";
$var_resolution = 1;
$var_color = 0;
$var_comments = "";
}
// SQL QUERY FOR MAIN MENU
$query = "SELECT * FROM tbl_content WHERE content_menu = 1 AND content_visible = 1 ORDER BY content_sortorder";
$rsmenu = mysql_query($query) or die("The query failed. ".mysql_error());
$num_rsmenu = mysql_num_rows($rsmenu);
$var_passedid = 5;
$query = "SELECT * FROM tbl_content WHERE content_id = $var_passedid AND content_visible = 1 ORDER BY content_sortorder";
$rscontent = mysql_query($query) or die("The query failed. ".mysql_error());
$num_rscontent = mysql_num_rows($rscontent)
;
if($num_rscontent == 0){ header("Location: index.php?id=0"); }
while ($row = mysql_fetch_array($rsconte
nt)){
$var_contentid = $row["content_id"];
$var_pagetitle = $row["content_pagetitle_" . $main__lang];
$var_content = $row["content_" . $main__lang];
$var_image = $row["content_image"];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php print $main__title; ?>
<?php if($var_pagetitle != ""){ print " | " . $var_pagetitle; } ?>
</title>
<link href="/includes/main.css" rel="stylesheet" type="text/css">
<script language="javascript">
<!--
function toggletype() {
if ( document.getElementById("s
el_type").
options[do
cument.get
ElementByI
d("sel_typ
e").option
s.selected
Index].val
ue == "0" ) {
document.getElementById('m
achined1')
.style.dis
play = '';
document.getElementById('m
achined2')
.style.dis
play = '';
document.getElementById('p
rototyped'
).style.di
splay = "none";
} else {
document.getElementById('p
rototyped'
).style.di
splay = '';
document.getElementById('m
achined1')
.style.dis
play = "none";
document.getElementById('m
achined2')
.style.dis
play = "none";
}
}
//-->
</script>
</head>
<body onLoad="toggletype();">
<form name="frm" method="post" enctype="multipart/form-da
ta" action="quote.php">
<?php include("includes/header.p
hp"); ?>
<table width="708" border="0" align="center" cellpadding="0" cellspacing="0" id="canvas">
<tr>
<td><img src="/content/banners/<?ph
p if($var_image == 1){ print $var_contentid; } else { print "0"; } ?>.jpg" width="700" height="216"><br>
<table width="700" border="0" cellpadding="0" cellspacing="0" id="menu">
<tr>
<?php
$var_ctr = 1;
while ($row = mysql_fetch_array($rsmenu)
){
?>
<td align="center"><a href="<?php if($row["content_url"] != ""){ print $row["content_url"]; } else { print "/index.php?id=" . $row["content_id"]; } ?>" class="menuitem"><?php print $row["content_menutitle_" . $main__lang]; ?></a></td>
<?php if($var_ctr < $num_rsmenu){ ?>
<td width="6"><img src="/images/divider.gif" width="6" height="50"></td>
<?php } ?>
<?php
$var_ctr += 1;
}
?>
</tr>
</table>
<br>
<table width="680" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="415"><?php if($var_pagetitle != "") { ?><span class="pagetitle"><?php print $var_pagetitle; ?></span><?php } ?><br><br>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<?php if($var_errornum == 1){ ?>
<tr>
<td colspan="2"><table width="360"><tr><td style="border: 1px dashed #990000; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; background-color:#C00101; color:#FFFFFF; padding: 5px"><?php print $var_errormsg; ?></td></tr></table></td>
</tr>
<tr><td> </td></tr>
<?php } ?>
<tr>
<td width="200" class="form_header" style="padding-bottom: 10px">First name*<br>
<input name="txt_fname" type="text" class="form_textbox" style="width: 150px" maxlength="40" value="<?php print $var_fname; ?>"></td>
<td width="194" class="form_header" style="padding-bottom: 10px">Last name*<br>
<input name="txt_lname" type="text" class="form_textbox" style="width: 150px" maxlength="50" value="<?php print $var_lname; ?>"></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Company name<br>
<input name="txt_company" type="text" class="form_textbox" style="width: 150px" maxlength="50" value="<?php print $var_company; ?>"></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Address*<br>
<input name="txt_address" type="text" class="form_textbox" style="width: 352px" maxlength="200" value="<?php print $var_address; ?>"></td>
</tr>
<tr>
<td class="form_header" style="padding-bottom: 10px">City*<br>
<input name="txt_city" type="text" class="form_textbox" style="width: 150px" maxlength="50" value="<?php print $var_city; ?>"></td>
<td class="form_header" style="padding-bottom: 10px">Province*<br>
<input name="txt_province" type="text" class="form_textbox" style="width: 150px" maxlength="50" value="<?php print $var_province; ?>"></td>
</tr>
<tr>
<td class="form_header" style="padding-bottom: 10px">Country*<br>
<input name="txt_country" type="text" class="form_textbox" style="width: 150px" maxlength="50" value="<?php print $var_country; ?>"></td>
<td class="form_header" style="padding-bottom: 10px">Postal code*<br>
<input name="txt_postal" type="text" class="form_textbox" style="width: 150px" maxlength="7" value="<?php print $var_postal; ?>"></td>
</tr>
<tr>
<td class="form_header" style="padding-bottom: 10px">Phone number* (555-555-5555)<br>
<input name="txt_phone" type="text" class="form_textbox" style="width: 150px" maxlength="12" value="<?php print $var_phone; ?>"></td>
<td class="form_header" style="padding-bottom: 10px">Extension<br>
<input name="txt_ext" type="text" class="form_textbox" style="width: 150px" maxlength="5" value="<?php print $var_ext; ?>"></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Email address*<br>
<input name="txt_email" type="text" class="form_textbox" style="width: 352px" maxlength="200" value="<?php print $var_email; ?>"></td>
</tr>
<tr>
<td colspan="2" style="padding-top: 15px; padding-bottom: 15px "><img src="images/bluedot.gif" width="356" height="1"></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Project type<br><select name="sel_type" id="sel_type" class="form_textbox" style="width: 356px" onChange="toggletype();">
<option value="0" <?php if($var_type == 0){ print "selected"; } ?>>Machined</option>
<option value="1" <?php if($var_type == 1){ print "selected"; } ?>>Rapid Prototyped</option>
</select></td>
</tr>
<tr id="prototyped">
<td colspan="2" class="form_header" style="padding-bottom: 10px">Material*<br>
<input name="txt_material" type="text" class="form_textbox" style="width: 352px" maxlength="50" value="<?php print $var_material; ?>"></td>
</tr>
<tr id="machined1">
<td colspan="2" class="form_header" style="padding-bottom: 10px">Resolution<br><selec
t name="sel_resolution" class="form_textbox" style="width: 356px">
<option value="0" <?php if($var_resolution == 0){ print "selected"; } ?>>Low</option>
<option value="1" <?php if($var_resolution == 1){ print "selected"; } ?>>High (Default)</option>
</select></td>
</tr>
<tr id="machined2">
<td colspan="2" class="form_header" style="padding-bottom: 10px">Color<br><select name="sel_color" class="form_textbox" style="width: 356px">
<option value="0" <?php if($var_color == 0){ print "selected"; } ?>>White (Default)</option>
<option value="1" <?php if($var_color == 1){ print "selected"; } ?>>Color (Extra charge may apply)</option>
</select></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Comments<br>
<textarea name="txt_comments" rows="5" wrap="virtual" class="form_textbox" style="width: 352px"><?php print $var_comments; ?></textarea></td>
</tr>
<tr>
<td colspan="2" class="form_header" style="padding-bottom: 10px">Attach a file* | <span class="form_value">Allowed
file types: .stl, .prt, .igs (solid)</span><br>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input name="myfile" type="file" class="form_textbox" style="width: 356px"></td>
</tr>
<tr>
<td colspan="2" class="form_button"><br>
<a href="javascript:document.
frm.submit
();" class="form_button">SEND</
a> <a
href="javascript:document.
frm.reset(
);" class="form_button">CLEAR<
/a></td>
</tr>
</table>
</td>
<td width="265" valign="top"><span class="content"><?php print $var_content; ?></span></td>
</tr>
</table>
<br>
<br>
</td>
</tr>
</table>
<table width="708" border="0" align="center" cellpadding="0" cellspacing="0" id="canvasfooter">
<tr>
<td> </td>
</tr>
</table>
<?php include("includes/footer.p
hp"); ?>
<input name="txt_firsttime" type="hidden" value="no">
</form>
</body>
</html>
Start Free Trial