this is how i saved an image to the database. I have a form which allows a logged in user to upload an image, with a title and description.
First, I created a new snippet called ’eform_processor’, which is referenced by the runSnippet parameter in my eForm call.
In this new processor snippet i have three functions, two of which are also called directly in the eform snippet call
I added these parameters are added to the eForm snippet call:
&eFormOnBeforeParseForm=`populate_hidden_fields`
&eFormOnBeforeMailSent=`add_to_db`
&runSnippet=`eform_processor`
I also created a new folder called ’uploads’ in the existing /assets/images folder to store the user uploaded images.
I also created a new table in the database called ’modx_web_user_uploads’ with the following columns:
’id, internalKey, title, message, image, entered’
The ’eform_processor’ snippet looks like this:
<?php
// this function adds some logged in user data to the form, so we can identify who uploaded it
function populate_hidden_fields (&$fields)
{
// get the users username, full name, and email to add as hidden fields in the form
$fields['username'] = $_SESSION['webShortname'];
$fields['fullname'] = $_SESSION['webFullname'];
$fields['email'] = $_SESSION['webEmail'];
}
// this is the function which saves the validated and posted data to the db,
// along with a reference to the filename we have saved in the uploads folder
function add_to_db(&$fields)
{
global $modx;
$uploads_table = $modx->getFullTableName('web_user_uploads');
// get the id of the user who posted this from their username
$res = $modx->db->select("id", $table_prefix.".modx_web_users",
"username='".mysql_real_escape_string($fields['username'])."'");
// providing a user was found, add the notice
if($modx->db->getRecordCount($res)) {
// first save the image
if(!empty($fields['image'])) {
$image_saved = save_image();
if(!$image_saved) {
echo ('image could not be saved');
}
else {
}
}
$row = $modx->db->getRow( $res );
$owner_id = $row['id'];
$data = array(
'internalKey' => $owner_id,
'title' => $modx->db->escape($fields['title']),
'message' => $modx->db->escape($fields['message']),
'image' => $image_saved
);
$insert_id = $modx->db->insert($data, $uploads_table);
}
// if the insert to database was done, send back to the gallery to view the lovely upload!
if($insert_id) {
// redirect back to gallery on success
$url = $modx->rewriteURLs('[~89~]');
$modx->sendRedirect('http://'.$_SERVER['HTTP_HOST'].'/'.$url);
} else {
echo '<p>Error inserting new notice</p>';
}
}
// this is the function which will save the image uploaded via the form, and give it a unique name
function save_image(){
// create a unique filename based on the date and time and files name, as two files could possibly have the same name
$uniq_name = date('ymdHi').'_'.basename($_FILES['image']['name']);
// Where the file is going to be placed
$target_path = "assets/images/upload/";
// Add the original filename to our target path.
$target_path = $target_path.$uniq_name;
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
return $uniq_name;
} else{
return false;
}
}
?>
Please note: this is basic code, which got my example going - you should secure the code in your version before use.
Any improvements are welcome!