PDA

View Full Version : File Upload/Menu Select Option Form


toad78
03-06-2009, 02:13 AM
My client wants me to combine a menu selection/file upload option form.

For example, say that they don't see the filename they want to use within the list and they want to upload the file instead when inserting the new record. I'm not sure how to combine the file upload/select process. Plus, I don't seem to understand the use of if/else when an error occurs.

I have this so far, but haven't gotten very far (I've gotten more frustrated than resolution). I am a rookie at this, so some helpful education would be grateful!

Thank you!
toad78

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
define('UPLOAD_DIR', '../../photos/staff/');
$file = str_replace(' ', '_', $_FILES['staff_photo']['name']);
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
$typeOK = false;


foreach ($permitted as $type) {
if ($type == $_FILES['staff_photo']['type']) {
$typeOK = true;
break;
}
}

if ($typeOK) {

switch($_FILES['staff_photo']['error']) {
case 0:
if (!file_exists(UPLOAD_DIR.$file)) {
$success = move_uploaded_file($_FILES['staff_photo']['name'], UPLOAD_DIR.$file);
}

if ($success) {
$insertSQL = sprintf("INSERT INTO staff (staff_name, staff_position, staff_subposition, staff_desc, staff_photo, staff_spec_1, staff_spec_2, staff_spec_3, staff_spec_4, staff_spec_5) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['staff_name'], "text"),
GetSQLValueString($_POST['staff_position'], "text"),
GetSQLValueString($_POST['staff_subposition'], "text"),
GetSQLValueString($_POST['staff_desc'], "text"),
GetSQLValueString($_POST['staff_photo'], "text"),
GetSQLValueString($_POST['staff_spec_1'], "text"),
GetSQLValueString($_POST['staff_spec_2'], "text"),
GetSQLValueString($_POST['staff_spec_3'], "text"),
GetSQLValueString($_POST['staff_spec_4'], "text"),
GetSQLValueString($_POST['staff_spec_5'], "text"));

mysql_select_db($database_wvgsadmin, $wvgsadmin);
$Result1 = mysql_query($insertSQL, $wvgsadmin) or die(mysql_error());

if ($Result1) {

$insertGoTo = "staff_list.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];

header(sprintf("Location: %s", $insertGoTo));
}
}
} else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
$result = "Error uploading $file. Please try again.";
default:
$result = "System error uploading $file. Contact webmaster.";
}

} else {
$result = "$file cannot be uploaded. Acceptable file type: .gif, .jpf, .png";
}
}

<form method="post" name="form1" enctype="multipart/form-data" action="<?php echo $editFormAction; ?>">
<table width="80%" align="center" cellspacing="0" border="0">
<tr valign="baseline">
<td nowrap align="right">Select Photo:</td>
<td><select name="staff_photo">
<option selected="selected"></option>
<?php buildImageList('../../photos/staff/'); ?>
</select>
<span class="small">&nbsp;&nbsp;&nbsp;(If there is no photo available, please choose 'No-Phot-Available.jpg' or 'No photo')</span></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Upload Photo: </td>
<td><input name="staff_photo" type="file" />
</td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Name:</td>
<td><input name="staff_name" type="text" class="widebox" value="" size="32" maxlength="150"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Primary Position:</td>
<td><input name="staff_position" type="text" class="widebox" value="" size="32" maxlength="150"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Support Position:</td>
<td><input name="staff_subposition" type="text" class="widebox" value="" size="32" maxlength="150"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right" valign="top">Biography:<br />
<span class="small">(use Return/Enter key<br />
to create a new line)</span></td>
<td><textarea name="staff_desc" cols="50" rows="5"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Insert record"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
<input name="staff_photo" type="hidden" id="staff_photo" />
</form>

davidj
03-06-2009, 09:39 AM
I am a rookie at this, so some helpful education would be grateful!



fancy some lessons how to code?