Page 1 of 1

php upload form details to mysql table

Posted: Thu Mar 12, 2009 8:09 pm
by bigurty
Hi

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">&nbsp;</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!

Re: php upload form details to mysql table

Posted: Fri Mar 13, 2009 8:40 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]

Re: php upload form details to mysql table

Posted: Sat Mar 14, 2009 5:11 am
by bigurty
thanks for your advice! i hope this is easier to read now.

thanks