We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11542
    • 1 Posts
    Hi - I have an eform upload form that works - sends the email with the file attached. However I’d like to save the file on the server and just send notification through rather than the file (each one is 10meg approx) -

    How can I do that? is it possible with eform or would I have to do something else (pointers please!)

    Cheers,

    Guy
      • 6038
      • 228 Posts
      hey - I’m trying to do the same thing myself at the minute. Searched on here, and people are referring to another snippet called ’eform2db’ which i can’t seem to find in the resources anymore??
      I think we have to write a custom script to handle the saving from a temp upload file, to another file of our choice.
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Yes, you would need to create a snippet to get the uploaded file and save it using one of eForm’s events.

        The original snippet, plus a lot of variations and discussion, can be found here http://modxcms.com/forums/index.php/topic,8111.15.html
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 6038
          • 228 Posts
          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!