PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Renamed file upload to update in custom post type

  1. Hi,

    I am needing some assistance with a custom post type and gravity forms. I have got everything working properly using the Gravity Forms + Custom Post Types plugin.

    I have an upload field where a file is uploaded and then this is put into a custom post type field with the url to the file. This seems to be working properly except one problem. I have added code to my functions.php file to rename the file after it uploaded (referencing this post http://www.gravityhelp.com/forums/topic/renaming-uploaded-files-before-saving )

    but when i view the post in the custom post type it still shows the original file name before it was renamed. Is it possible to have this automatically update in the custom post type as well?

    Any help would be greatly appreciated!
    Thank you.

    Posted 12 years ago on Monday October 22, 2012 | Permalink
  2. Please share the code you are using the rename the uploaded file. We can incorporate changing the custom field after the form submission using the gform_after_submission hook.

    http://www.gravityhelp.com/documentation/page/Gform_after_submission

    (The old example used gform_post_submission, but that hook is deprecated and gform_after_submission is the replacement.)

    We will need to know the meta key name of the field where you are storing the file name so we know what to update.

    Posted 12 years ago on Thursday October 25, 2012 | Permalink
  3. jessicasantacruz
    Member

    Hi Chris,
    I am new to Gravity Forms and was able to rename my upload file using the info in this post:
    http://www.gravityhelp.com/forums/topic/file-upload-location-and-name#post-76119. It looks great when I log into the WP admin and view the entry, but the problem is that the file name and link in the notification email sent to the client is not updated with the new file name.

    Please let me know if there is a way to change the link and filename within the notification also.

    Thank You for your help,
    Jessica

    Posted 11 years ago on Friday January 11, 2013 | Permalink
  4. The gform_after_submission code will be run after the form is submitted, the entry is created, and the notifications are sent. That is probably not what you're looking for.

    Can you share a link to the form embedded on your site so we can see how many files you need to rename and what the notification looks like now?

    Posted 11 years ago on Friday January 11, 2013 | Permalink
  5. jessicasantacruz
    Member

    Hi Chris,
    Here is where the form is currently:
    http://awo.aws.org/professional-program-abstract-form/

    It's just been uploaded a few days ago so I do not have many files to rename at this point, but would like to have this functionality in place for the future.

    Thanks Chris,
    Jessica

    Posted 11 years ago on Saturday January 12, 2013 | Permalink
  6. You won't be able to change the file name with the gform_after_submission code since the notifications have already been sent out. You could use the gform_pre_submission_filter and work with the $_FILES global, which will contain the file name and the temp directory where the image is located before it's moved. However, I have not seen anyone post the code to perform that customization and I've never done it myself. Can you post the code you are using the change the file name now so we can see what you're trying to accomplish?

    Posted 11 years ago on Thursday January 17, 2013 | Permalink
  7. jessicasantacruz
    Member

    Hi Chris,
    I am using the exact code from this link:
    http://www.gravityhelp.com/forums/topic/file-upload-location-and-name#post-76119
    I just changed the references to form 9 to 1.

    I would like to have the files renamed using the id# because the uploaded files from each submission are tests that need to be evaluated anonymously as well as easily linked to the database submission. The uploaded file that is attached to the notification email is what my client would like to use as her reference to the file rather than logging into the WordPress admin since she is not involved with the site itself in any other way.

    Please let me know if you need anything further from me.

    Thanks,
    Jessica

    Posted 11 years ago on Friday January 25, 2013 | Permalink
  8. I'm not sure how you can do this then, since the gform_after_submission code will run after the notifications have been sent. Did you come up with any other workarounds?

    Posted 11 years ago on Wednesday January 30, 2013 | Permalink
  9. Hi Sorry for the extremely late response. Here is my code that I am using for this:

    add_action("gform_post_submission", "rename_file", 10, 2);
    function rename_file($entry, $form){
    
        //---------------------------------------------------------
        //---------------------------------------------------------
        //REPLACE THESE THREE VARIABLES BASED ON YOUR ACTUAL IDs
        $form_id = "1";
        $fileupload_field_id = "6";
        $name_field_id = "1";
        //---------------------------------------------------------
        //---------------------------------------------------------
    
        //ignore other forms
        if($form["id"] != $form_id)
            return;
    
        $upload_info = wp_upload_dir();
    
        //original name/path
        $original_file_url = $entry[$fileupload_field_id];
        $original_file_path = str_replace($upload_info["baseurl"], $upload_info["basedir"], $original_file_url);
        $original_file_info = pathinfo($original_file_url);
    
        //New File Name (without extension).
    	$nospaces = $entry[$name_field_id];
    	$nospaces = ucwords(strtolower($nospaces));
    	$nospaces = str_replace(' ', '', $nospaces);
        $new_name = "TS-" .  $nospaces . "_" . $entry["id"];
    
        //adding same extension as original
        $new_name .= "." . $original_file_info["extension"];
    
        $new_file_url = str_replace($original_file_info["basename"], $new_name, $original_file_url);
        $new_file_path = str_replace($original_file_info["basename"], $new_name, $original_file_path);
    
        //rename file
        $is_success = rename($original_file_path, $new_file_path);
    
        //if file was renamed successfully, updating entry so that it points to the new file
        if($is_success){
            global $wpdb;
            $wpdb->update(RGFormsModel::get_lead_details_table_name(), array("value" => $new_file_url), array("lead_id" => $entry["id"], "value" => $original_file_url));
        }
    }

    The Meta name for the field in the custom post type is "sample-upload"

    Thank you

    Posted 11 years ago on Wednesday February 6, 2013 | Permalink
  10. Anyone will to give me a hand with this? :)

    Posted 11 years ago on Thursday February 21, 2013 | Permalink
  11. Wired
    Member

    I've just hit this problem too. The previously published solution is complex, and involves modifying the DB after the lead record has been written. This also - as we now know - takes place too far down the track to affect anything in the notification emails and - often - data sent to other plugins.

    Instead, as suggested above, I hooked into the much earlier "gform_pre_submission_filter", which triggers when the form is POSTed back, and before any processing is done. The code below is pretty basic (it'll run on every form - if you need something more specific, add a form filter check at the start) and just loops through the $_FILES array, replacing any filename it finds with a pseudo-random MD5 hash. (You could, obviously, replace this with any algorithm you wish to generate the filename.)

    However, remember that as this happens long before the DB write, you don't have access to a Lead ID or any other DB-related data; any other submitted form data you want to use will have to be got the old-fashioned way directly from $_POST - be careful to properly escape any field you plan to redisplay or use in an unexpected way to avoid the creation of potential XSS or SQL-injection vulnerabilities.

    // Force uploaded filenames to a unique hash BEFORE form is processed
    add_filter("gform_pre_submission_filter", "wired_set_upload_filenames", 10, 1);
    function wired_set_upload_filenames($form)
    {
     	foreach($_FILES as &$file)
    	{
    		$oldname = $file['name'];
    		$newname = $oldname;
    		$ext = '';
    		$dot = strrpos($oldname,'.');
    		if ( false !== $dot )
    		{
    			$ext = substr($oldname,$dot);
    			$newname = md5( mt_rand().$oldname );
    		}
    		$file['name'] = $newname . $ext;
    	}
        // Return form object
        return $form;
    }

    Hope this is of help to others in the same boat.

    Posted 11 years ago on Wednesday May 1, 2013 | Permalink