I am creating a page which allows users to upload files. the uploaded files are stored in a folder, but i would also like the form details to be recorded into a table and wondered if it was possible. i have searched the net and have foudn various codes, but have failed to achieve my aim.
i would appreciate any help!
the following is the code im using on my page in DreamWeaver CS3:
Code: Select all
<?php
// define constant which contains the maximum file size in bytes
define('MAX_FILE_SIZE', 4000000);
if (array_key_exists('btn', $_POST)) {
// define new constant which contains the path the the upload folder
define('UPL_FLD','uploads/');
//Find the extension
$flext = pathinfo($_FILES['frmfile']['name']);
$ext = strtolower($flext['extension']);
// create new file name
$file = str_replace(' ', '_', $_POST['frmname'].'.'.$ext);
$file = strtolower($file);
// create variable and assign the formatted value of MAX_FILE_SIZE to it
$maxfs = number_format(MAX_FILE_SIZE/1024, 1).'KB';
$fsize = false;
// check the file size
if ($_FILES['frmfile']['size'] > 0 && $_FILES['frmfile']['size'] <= MAX_FILE_SIZE) {
$fsize = true;
}
// allow MIME file types
$filetype = array('image/gif','image/jpeg','image/pjpeg','image/png');
$ftype = false;
// check if uploaded file type is allowed
foreach($filetype as $type) {
if ($type == $_FILES['frmfile']['type']) {
$ftype = true;
break;
}
}
if ($ftype && $fsize && $_POST['frmname'] != '') {
switch($_FILES['frmfile']['error']) {
case 0:
// move file to the upload folder
$upload = move_uploaded_file($_FILES['frmfile']['tmp_name'],UPL_FLD.$file);
if ($upload) {
$msg = $_FILES['frmfile']['name'].' uploaded successfully';
} else {
$msg = 'Error.<br />Please try again.';
}
break;
case 3:
$msg = 'Error.<br />Please try again.';
break;
default:
$msg = 'Error - please contact administrator';
}
} elseif ($_FILES['frmfile']['error'] == 4) {
$msg = 'Please select file';
} elseif ($_POST['frmname'] == '') {
$msg = 'Please provide your full name';
} else {
$msg = $_FILES['frmfile']['name'].' cannot be uploaded.<br />';
if(!$ftype) {
$msg .= 'Acceptable file formats are: .gif, .jpg, .png<br />';
}
if(!$fsize) {
$msg .= 'Maximum file size is '.$maxfs;
}
}
}
php?>Code: Select all
<?php if(isset($msg)) { echo '<p class="warning">'.$msg.'</p>'; } ?>
<form action="" method="post" enctype="multipart/form-data" name="frm_upload" id="frm_upload">
<table border="0" cellspacing="0" cellpadding="0" id="tbl_upload">
<tr>
<th scope="row"><label for="frmname">Username:</label></th>
<td><input type="text" name="frmname" id="frmname" class="frmfld" /></td>
</tr>
<tr>
<th scope="row"><label for="frmname">Full name:</label></th>
<td><input type="text" name="frmname" id="frmname" class="frmfld" /></td>
</tr>
<tr>
<th scope="row"><label for="frmname">Coursework Title:</label></th>
<td><input type="text" name="frmname" id="frmname" class="frmfld" /></td>
</tr>
<tr>
<th scope="row"><label for="frmfile">File:</label></th>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input name="frmfile" type="file" id="frmfile" size="30" /></td>
</tr>
<tr>
<th scope="row"> </th>
<td>
<label for="btn" id="sbm">
<input type="submit" name="btn" id="btn" value="Upload" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
Now if possible, what code and where can i add this code to the above script i am using to allow the form details to be saved in a table. I also have not created a table for the form details to be saved in as i wasnt sure the best way to approach this.
Thank you for your help.
Cheers!