Please Note: We are no longer using forums to facilitate standard support. 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.

Create month and year based folders with Gform_upload_path

  1. sascha

    Hi there,
    is there a way to create year and month based folders with the Gform_upload_path filter? Obviously the folders need to be created as they will change ever so often.
    Ideally I would base the upload folder on:
    form title/year/month/user_role/user_name

    The main thing I need to know is how to create the folders. The rest I can put together in a function. But a little example based on the form title for instance will get me going the right way!
    Many thanks,

    Posted 3 years ago on Tuesday November 22, 2011 | Permalink
  2. sascha

    Hi again,
    folders do get created automatically. So that is all good. How do I go about getting the "form title" and the "post title" (I am submitting a post), so I can add those to my function for the filter?

    Posted 3 years ago on Tuesday November 22, 2011 | Permalink
  3. sascha, please start a new topic for your request to get the form title and post title. Those issues are unrelated to this topic, and it makes searching for solutions hard when they're buried in an unrelated topic like this. We'll be happy to help you with your request. Thank you.

    Posted 3 years ago on Wednesday November 23, 2011 | Permalink
  4. Hi, Sascha,

    In response to your first question, the link below will give you an example of how to create the new folders for the form title, year, month, user level, and user name using the gform_upload_path hook.

    As far as getting the post title, this is not possible using the gform_upload_path hook because the post is not created when this hook fires.

    Let me know if you have questions about the code provided.

    Have fun!

    Posted 3 years ago on Wednesday November 23, 2011 | Permalink
  5. sascha

    Hi Dana,
    thanks for the code! It is very clean and easy to understand, thanks to all your comments!
    Is there any way to get a form field by ID then ? I can get the "post title" by doing that. I just have to make sure the post title field is always the same ID in each form or create filters for different forms (which I might do anyway).

    One question though:

    Why do you get the form model object? You do not seem to use it?
    $form = RGFormsModel::get_form_meta($form_id); //get form model object
    Is all the form data in there?

    Or would I need to get the entry model object? If so, how would I go about this?

    Other question: should I open a new ticket again (other one was closed) or is it enough that the other ticket points to this one?

    Posted 3 years ago on Friday November 25, 2011 | Permalink
  6. sascha

    Hello Dana again,

    Other "dirty" option maybe???
    I know the post title is available during:
    public static function create_post($form, &$lead) (in forms_model.php)
    I saved it once (some months ago) adding at around 1484 - just before //inserting post

    //SASCHA defining variables------------------------------------------------------------------------------------------xxxxxxxxxxxxxxxxxxxxxxxxxxxx-----------------------
    		global $post_title_from_form;
    		$post_title_from_form = $post_data["post_title"];

    It's not ideal, but I do not mind adding that code on each update :)
    That $post_title_from_form is then available in get_file_upload_path (where the gform_upload_path filter is located) at around 1885 when I insert:
    global $post_title_from_form; and then insert/replace:

    $target_root = self::get_upload_path($form_id) . "/$currentuserrole/$currentusername/$formtitle/$post_title_from_form/";
    $target_root_url = self::get_upload_url($form_id) . "/$currentuserrole/$currentusername/$formtitle/$post_title_from_form/";

    Before I knew about the filter I "hardcoded" that path using $post_title_from_form as above (after I cleaned it up of course). Again that is not ideal! I rather use the filter!
    Is there a way of somehow making that variable ($post_title_from_form) available in your filter function change_upload_path from your snippet? Then all I need to do is put in those lines defining that variable each time I update...

    Anyway, any pointers would be great, as I am "desperate" to keep the uploaded files/images together in a directory based on the post title!

    Posted 3 years ago on Friday November 25, 2011 | Permalink
  7. sascha

    Hi Dana,
    of course you used the form object model! Sometimes I'm just blind...
    And of course I need to get a field value from the current form by field ID.
    Have a great day,

    Posted 3 years ago on Friday November 25, 2011 | Permalink
  8. Hi, Sascha,

    I was off for the holiday and took a look at your responses today. I wasn't sure if you still needed some help because it looked like you answered a lot of your own questions. If you do still have questions that aren't specific to creating folders using the month/year as the topic title mentions, I think you should create a new topic as Chris suggested. This way others will benefit when searching for issues related to your new topic which may otherwise be buried in the upload folder creation topic.


    Posted 3 years ago on Tuesday November 29, 2011 | Permalink
  9. sascha

    Thanks for getting back. In the end I used this code (if someone wants to do something similar):

    //GF to change upload path for forms
    add_filter("gform_upload_path", "change_upload_path", 10, 2);
    function change_upload_path($path_info, $form_id){
        $form = RGFormsModel::get_form_meta($form_id);  //get form model object
    	$post_category_id = $form["postCategory"]; //I use this to set the post type category
    	$post_category_name = get_cat_name($post_category_id);
    	echo $form["postCategory"];
    	echo $post_category_name;
        //$upload_dir = wp_upload_dir(); //get wordpress upload directory
        //$dir_base = $upload_dir["basedir"] . "/gravity_forms/";
        //$url_base = $upload_dir["baseurl"] . "/gravity_forms/";
        	$url_base = site_url() . "/gf_submissions/"; // use directory outside WP - needs the "/" at the beginning
       	$dir_base = ABSPATH . "gf_submissions/"; // use directory outside WP - does not need the "/" at the beginning
        $current_user = wp_get_current_user(); //get current wordpress user information
        if ( 0 == $current_user->ID ) {
          //user is not logged in; set default values
          $user_level = "not_logged_in";
          $user_login = "not_logged_in";
        else {
          //user is logged in
          $user_role = get_current_user_role(); // calls my own function -> get_current_user_role
          $user_login = $current_user->user_login;
    	//get post title from form ----- be careful with field ID!!!
    	$post_title = $_POST['input_1'];
    	//cleaning it - not sure if that is a great way?????
    	$post_title = trim($post_title);
    	$post_title = preg_replace('<code>[^a-z0-9_]</code>i','',strtolower(ereg_replace(" ","_",$post_title)));
    	$post_title = trim(ereg_replace( '_+', '_', $post_title));
        //set variable to form title, 4 digit year, 2 digit month, user level (numeric), user login
        //$subfolders = $form["title"]."/".date("Y")."/".date("m")."/".$user_level."/".$user_login."/";
        	$subfolders = $post_category_name."/".$user_role."/".$user_login."/".$post_title."/";
    	$path_info["path"] = $dir_base.$subfolders; //set path
        $path_info["url"] = $url_base.$subfolders; //set url
        return $path_info;
    Posted 3 years ago on Wednesday November 30, 2011 | Permalink

This topic has been resolved and has been closed to new replies.

Support Tickets

Open A New Support Ticket
 Open A New Priority Support Ticket *

* Developer license holders only