FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

File Upload / Abstracts submission in a page + email + db

Feed 1 posts, 1 voices

Avatar
5 posts

This is the way I used to make a form with a file upload. The form is saved into the database and the file will be uploaded to the server. Both form and file will be sent by email and a confirmation email will be sent to the customer.

This is a really quick and dirty dev, but I share it here in case someone have time to make it into a clean plugin.

What you need:

  1. add the swift mail library
  2. add a table to your database
  3. Create the confirmation page
  4. Create the submission form
  5. Modifiy the file
  6. Test
  7. ToDo

1) add the swift mail library

  • go to http://swiftmailer.org/ and download package
  • the one I use is the Swift-4.0.6.tar.gz
  • extract the swift library to /frog/libraries/swift

2) add a table to your database

CREATE TABLE `submissions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `last_name` varchar(255) DEFAULT NULL, `first_name` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `url` varchar(255) DEFAULT NULL, `address` text, `zip` varchar(10) DEFAULT NULL, `city` varchar(255) DEFAULT NULL, `country` varchar(255) DEFAULT NULL, `affiliation` varchar(255) DEFAULT NULL, `aff_url` varchar(255) DEFAULT NULL, `presentation_type` varchar(10) DEFAULT NULL, `presenter_type` varchar(20) DEFAULT NULL, `attent_type` varchar(10) DEFAULT NULL, `room_type` varchar(10) DEFAULT NULL, `full_manuscript` varchar(3) DEFAULT NULL, `upfile` varchar(255) DEFAULT NULL, `date` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

3) Create the confirmation page

  • Title: You submission has been received
  • Content:

<center><b>Thank you,<br/>A confirmation email has been sent.</b></center>

  • Status: hidden

4) Create the submission form
<?php

$upload_dir = FROG_ROOT . ‘/uploads/’;

// —————————————————————————————————————————
// First part of file -> FORM HANDLE
// —————————————————————————————————————————

$errors = array();

// Clean vars
foreach ($_POST as $pkey => $pval) { $_POST[$pkey] = makeSafe($pval) ;
}

if (trim($_POST[‘first_name’]) == ‘’) { $errors[‘first_name’] = ‘First Name must be provided’ ;
}

if (trim($_POST[‘last_name’]) == ‘’) { $errors[‘last_name’] = ‘Last Name must be provided’ ;
}

if (trim($_POST[‘email’]) == ‘’) { $errors[‘email’] = ‘Email must be provided’ ;
}

elseif (isValidEmail($_POST[‘email’]) === false)
{ $errors[‘email’] = ‘Email must be valid’ ;
}

if (trim($_POST[‘address’]) == ‘’) { $errors[‘address’] = ‘Address must be provided’ ;
}

if (trim($_POST[‘city’]) == ‘’) { $errors[‘city’] = ‘City must be provided’ ;
}

if (trim($_POST[‘country’]) == ‘’) { $errors[‘country’] = ‘Country must be provided’ ;
}

if (trim($_POST[‘presentation_type’]) == ‘’) { $errors[‘presentation_type’] = ‘Presentation type must be provided’ ;
}

if (trim($_POST[‘presenter_type’]) == ‘’) { $errors[‘presenter_type’] = ‘Presenter type must be provided’ ;
}

if (trim($_POST[‘attent_type’]) == ‘’) { $errors[‘attent_type’] = ‘Attent type must be provided’ ;
}

if (isset($_FILES)) {

if (isset($_FILES[‘upfile’][‘error’])) {

// check http://www.php.net/manual/en/features.file-upload.errors.php

/* UPLOAD_ERR_OK Value: 0; There is no error, the file uploaded with success.

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0.

*/

if ($_FILES[‘upfile’][‘error’] == 0) { // ok

$upload_ori_name = basename($_FILES[‘upfile’][‘name’]);

$date = date(‘Ymd_his’);

$upload_ori_dest_name = $date .’_’. $upload_ori_name ;

$upload_ori_dest = $upload_dir .‘ori/’. $upload_ori_dest_name ;

if (move_uploaded_file($_FILES[‘upfile’][‘tmp_name’], $upload_ori_dest)) {

// echo “File is valid, and was successfully uploaded.\n”;

} else {

// TODO: fine error handling

$errors[‘upfile’] = ‘Error while moving uploaded file’ ;

}

} else { $errors[‘upfile’] = ‘Abstract must be provided – error upload #’.$_FILES[‘upfile’][‘error’] ; } } else { $errors[‘upfile’] = ‘Abstract must be provided’ ; }

} else {

$errors[‘upfile’] = ‘Abstract must be provided’ ;

}

if (count($errors) != 0 && count($_POST) != 0) {

print ‘<h3 style=“color:red;”>Please correct these errors</h3>’;

print “<ul>”;

foreach ($errors as $errk => $errv) {

print “<li>”.$errv.”</li>”;

}

print “</ul>”;

}

// —————————————————————————————————————————
// Second part of file -> FORM
// —————————————————————————————————————————

// do not print form is submit is ok
if (count($errors) != 0) {
?>
<form name=“form_submissions” id=“form_submissions” action=”<? echo $_SERVER[‘PHP_SELF’].’?’.$_SERVER[‘QUERY_STRING’]; ?>” enctype=“multipart/form-data” method=“POST”>
<table width=“100%” cellpadding=“10” cellspacing=“0” style=“border-collapse: collapse;”> <tr> <td colspan=“2” align=“left”><hr /><h4>About you</h4></td> </tr> <tr> <td width=“45%” align=“right”>Title&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“title” id=“title” value=”<?php echo $_POST[‘title’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* First Name&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“first_name” id=“first_name” value=”<?php echo $_POST[‘first_name’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* Last Name&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“last_name” id=“last_name” value=”<?php echo $_POST[‘last_name’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* Email&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“email” id=“email” value=”<?php echo $_POST[‘email’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>URL&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“url” id=“url” value=”<?php echo $_POST[‘url’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* Address&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><textarea name=“address” id=“address” rows=“3” cols=“20”><?php echo $_POST[‘address’]; ?></textarea></td> </tr> <tr> <td width=“45%” align=“right”>Post Code&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“zip” id=“zip” value=”<?php echo $_POST[‘zip’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* City&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“city” id=“city” value=”<?php echo $_POST[‘city’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>* Country&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“country” id=“country” value=”<?php echo $_POST[‘country’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>Affiliation&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“affiliation” id=“affiliation” value=”<?php echo $_POST[‘affiliation’]; ?>” /></td> </tr> <tr> <td width=“45%” align=“right”>URL&nbsp;:&nbsp;</td> <td width=“55%” align=“left”><input type=“text” name=“aff_url” id=“aff_url” value=”<?php echo $_POST[‘aff_url’]; ?>” /></td> </tr> <tr> <td colspan=“2” align=“left”><br /><hr /><h4>About your intention</h4></td> </tr> <tr> <td width=“45%” align=“right”>I would like to submit&nbsp;:&nbsp;</td> <td width=“55%” align=“left”> <select name=“presentation_type” id=“presentation_type”> <option></option> <option value=“oral” <?php echo ($_POST[‘presentation_type’]==‘oral’)?‘selected=“selected”’:’‘ ; ?>>an oral presentation</option> <option value=“poster” <?php echo ($_POST[‘presentation_type’]==‘poster’)?‘selected=“selected”’:’‘ ; ?>>a poster</option> </select> </td> </tr> <tr> <td width=“45%” align=“right”>I am&nbsp;:&nbsp;</td> <td width=“55%” align=“left”> <select name=“presenter_type” id=“presenter_type”> <option></option> <option value=“presenting_author” <?php echo ($_POST[‘presenter_type’]==‘presenting_author’)?‘selected=“selected”’:’‘ ; ?>>presenting author</option> <option value=“coauthor” <?php echo ($_POST[‘presenter_type’]==‘coauthor’)?‘selected=“selected”’:’‘ ; ?>>coauthor</option> </select> </td> </tr> <tr> <td width=“45%” align=“right”>I will be&nbsp;:&nbsp;</td> <td width=“55%” align=“left”> <select name=“attent_type” id=“attent_type”> <option></option> <option value=“regular” <?php echo ($_POST[‘attent_type’]==‘regular’)?‘selected=“selected”’:’‘ ; ?>>a regular attendant</option> <option value=“student” <?php echo ($_POST[‘attent_type’]==‘student’)?‘selected=“selected”’:’‘ ; ?>>a student attendant</option> <option value=“invited” <?php echo ($_POST[‘attent_type’]==‘invited’)?‘selected=“selected”’:’‘ ; ?>>invited speaker</option> <option value=“not_att” <?php echo ($_POST[‘attent_type’]==‘not_att’)?‘selected=“selected”’:’‘ ; ?>>not attending</option> </select> </td> </tr> <td width=“45%” align=“right”>I will&nbsp;:&nbsp;</td> <td width=“55%” align=“left”> <select name=“room_type” id=“room_type”> <option></option> <option value=“single” <?php echo ($_POST[‘room_type’]==‘single’)?‘selected=“selected”’:’‘ ; ?>>need a single room</option> <option value=“double” <?php echo ($_POST[‘room_type’]==‘double’)?‘selected=“selected”’:’‘ ; ?>>share a double room</option> </select> </td> <tr> <td width=“45%” align=“right”>I intend to submit a full manuscript to the Journal &nbsp;:&nbsp;</td> <td width=“55%” align=“left” valign=“top”>&nbsp;&nbsp;yes <input type=“radio” name=“full_manuscript” id=“full_manuscript_yes” value=“yes” <?php echo ($_POST[‘full_manuscript’]==‘yes’)?‘checked=“true”’:’‘ ; ?>/> / no <input type=“radio” name=“full_manuscript” id=“full_manuscript_no” value=“no” <?php echo ($_POST[‘full_manuscript’]==‘no’)?‘checked=“true”’:’‘ ; ?>/></td> </tr> <tr> <td colspan=“2” align=“left”><br /><hr /><h4>Your abstract</h4></td> </tr> <tr> <td width=“45%” align=“right”>File to upload&nbsp;:&nbsp;</td> <td width=“55%” align=“left”> <input type=“hidden” name=“MAX_FILE_SIZE” value=“250000000” /> <input type=“file” name=“upfile” id=“upfile” /></td> </tr> <tr> <td width=“45%” align=“right”>&nbsp;</td> <td width=“55%” align=“left”></td> </tr> <tr> <td width=“45%” align=“right”>&nbsp;</td> <td width=“55%” align=“left”><input type=“submit” name=“submit” id=“submit” value=“submit” /></td> </tr>
</table>

</form>

<?php
}

// —————————————————————————————————————————
// Third part of file -> MAIL HANDLE
// —————————————————————————————————————————
?>
<?php
if (count($errors) == 0) {
$path_parts = pathinfo($upload_ori_dest);
$filename_without_date = ltrim($path_parts[‘filename’], $date);
$complete_name = $_POST[‘last_name’] . “” . $_POST[‘first_name’]0 . “_” . $filename_without_date;
$clean_name = filter($complete_name);
$final_name = $date . “_” . $clean_name .’.’. $path_parts[‘extension’] ;

$final_dest = $upload_dir . $final_name ;

if (!copy($upload_ori_dest, $final_dest)) { echo “failed to copy $file…\n”; die();
} else { // print “OK! copy “ . $upload_ori_des . “ to “ . $final_dest;
}

// preparing the mail body:
$mail_body = “<h3>Submitter infos</h3>
<p>Title : {$_POST[‘title’]}</p>
<p>First Name : {$_POST[‘first_name’]}</p>
<p>Last Name : {$_POST[‘last_name’]}</p>
<p>Email : {$_POST[‘email’]}</p>
<p>URL : {$_POST[‘url’]}</p>
<p>Address : {$_POST[‘address’]}</p>
<p>Post Code : {$_POST[‘zip’]}</p>
<p>City : {$_POST[‘city’]}</p>
<p>Country : {$_POST[‘country’]}</p>
<p>Affiliation : {$_POST[‘affiliation’]}</p>
<p>URL : {$_POST[‘aff_url’]}</p>

<h3>Submitter intentions</h3>
<p>I would like to submit : {$_POST[‘presentation_type’]}</p>
<p>I am : {$_POST[‘presenter_type’]}</p>
<p>I will be : {$_POST[‘attent_type’]}</p>
<p>I intend to submit a full manuscript to the Journal : {$_POST[‘full_manuscript’]}</p>

<h3>Submission info</h3>
<p>Date : {$date}</p>
<p>Original name : {$path_parts[‘basename’]}</p>
<p>Final name : {$final_name}</p>”;

require_once CORE_ROOT.’/libraries/swift/lib/swift_required.php’;

//Create the Transport
$transport = Swift_SmtpTransport::newInstance(‘smtp.mail.com’, 25);

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create the message
$message = Swift_Message::newInstance()

//->setCharset(‘utf-8’) //Give the message a subject ->setSubject(‘New submission – ‘ . $path_parts[‘filename’] .’.’. $path_parts[‘extension’]) //Set the From address with an associative array ->setFrom(array(‘from_email@mail.com’ => ‘system mailer’)) //Set the To addresses with an associative array ->setTo(array(‘to_email@mail.com’, ‘2_to_email@mail.com’ => ‘the to email 2 name’)) //Give it a body $mail_body ->setBody(‘read it in html please’) //And optionally an alternative body // ->addPart(’<q>Here is the message itself</q>’, ‘text/html’) ->addPart($mail_body, ‘text/html’) //Optionally add any attachments ->attach(Swift_Attachment::fromPath($final_dest)) ;

//Send the message
//$result = $mailer->send($message);

if ($mailer->send($message))
{

// Confirmation email content
$mail_body_usr = “<h3>Submission acknowledgement</h3><p>Dear {$_POST[‘first_name’]} {$_POST[‘last_name’]},<br>&nbsp;&nbsp;&nbsp;&nbsp;your submission has been successfully received.</p><p><br/>Best regards, <br/>System automatic mail.<br/></p>”;

//Create the message
$message_usr = Swift_Message::newInstance()

//->setCharset(‘utf-8’) //Give the message a subject ->setSubject(‘Your submission has been received’) //Set the From address with an associative array ->setFrom(array(‘from_email@mail.com’ => ‘system mailer’)) //Set the To addresses with an associative array ->setTo(array($_POST[‘email’] => $_POST[‘last_name’] . “ “ . $_POST[‘first_name’])) // Check it with Bcc ->setBcc(array(‘bcc_email@mail.com’ => ‘no used’)) //Give it a body $mail_body ->setBody(‘read it in html please’) //And optionally an alternative body // ->addPart(’<q>Here is the message itself</q>’, ‘text/html’) ->addPart($mail_body_usr, ‘text/html’) //Optionally add any attachments //->attach(Swift_Attachment::fromPath($final_dest)) ;

if ($mailer->send($message_usr)) { echo “<h2>You submission has been received</h2>”; echo “<p>A confirmation email has been sent.</p>”;

if (save_submission($_POST, $final_dest) !== true) { die(‘error saving data’); } else { $url_conf = URL_PUBLIC . ‘/?you-submission-has-been-received.html’ ; header(“Location: $url_conf”); }

} else { die(‘error’);
}

} else { echo “Failed\n”;
}

}

// —————————————————————————————————————————
// Last part of file -> FUNCTIONS
// —————————————————————————————————————————

function filter($in)
{ $search = array (’@[éèêëÊË]@i’,’@[àâäÂÄ]@i’,’@[îïÎÏ]@i’,’@[ûùüÛÜ]@i’,’@[ôöÔÖ]@i’,’@[ç]@i’,’@[ ]@i’,’@[^a-zA-Z0-9_]@’); $replace = array (‘e’,‘a’,‘i’,‘u’,‘o’,‘c’,’_’,’‘); return preg_replace($search, $replace, $in);
}

function isValidEmail($email){ return eregi(”^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email);
}

function makeSafe($data)
{ return trim(addslashes(htmlentities(htmlspecialchars(strip_tags($data),ENT_QUOTES,“UTF-8”))));
}

function save_submission($values, $file)
{

$insert_ary = array( ‘title’ => $values[‘title’], ‘last_name’ => $values[‘last_name’], ‘first_name’ => $values[‘first_name’], ‘email’ => $values[‘email’], ‘url’ => $values[‘url’], ‘address’ => $values[‘address’], ‘zip’ => $values[‘zip’], ‘city’ => $values[‘city’], ‘country’ => $values[‘country’], ‘affiliation’ => $values[‘affiliation’], ‘aff_url’ => $values[‘aff_url’], ‘presentation_type’ => $values[‘presentation_type’], ‘presenter_type’ => $values[‘presenter_type’], ‘attent_type’ => $values[‘attent_type’], ‘room_type’ => $values[‘room_type’], ‘full_manuscript’ => $values[‘full_manuscript’], ‘upfile’ => $file );

$config_file = FROG_ROOT.’/config.php’; require_once($config_file); $dbh = new PDO;

$sql = ‘insert into submissions (title, last_name, first_name, email, url, address, zip, city, country, affiliation, aff_url, presentation_type, presenter_type, attent_type, room_type, full_manuscript, upfile) values (:title, :last_name, :first_name, :email, :url, :address, :zip, :city, :country, :affiliation, :aff_url, :presentation_type, :presenter_type, :attent_type, :room_type, :full_manuscript, :upfile)’;

$stmt = $dbh->prepare($sql);

return $stmt->execute($insert_ary);

}

5) Modifiy the file

  • upload dir
  • Swift_SmtpTransport
  • search @mail.com to change emails
  • url to you-submission-has-been-received.html page

6) Test

  • Now you can try to access the submissions page.

7) ToDo

  • clean
  • check caracter encoding
  • improve security
  • make all variables more easy to change
  • make a plugin

 
Avatar
5 posts

If someone can tell me how to easily layout a page of code….