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.

Renaming File Uploads?

  1. Hey folks,

    With file uploads, is it possible to rename the file during the upload process to include some of the form fields content?

    Say there's the name form field with FIRSTNAME and SURNAME and I'm uploading biography.doc.

    Is it possible to rename the file on upload to biography_SURNAME_FIRSTNAME.doc to try avoid having similar names. At present, if I upload two files called biography.doc it will go down the wordpress renaming route of adding a digit to the end of the filename so I wind up with biography.doc and biography1.doc.

    Posted 14 years ago on Monday December 21, 2009 | Permalink
  2. I'm going to talk to the team about this. I'm not sure if this is changeable using one of the API hooks, I don't believe it is. If it is not we may be able to add a hook that will allow you to have some control over this.

    Posted 14 years ago on Monday December 21, 2009 | Permalink
  3. Thanks Carl.

    Also noticed that uploading files that have apostrophes (') in them e.g mary's%20file.ext would break the link sent to admins in email to download form attachments, another incentive (for me anyway) to look at renaming files on upload.

    Looking forward to seeing if any outcome. Really liking the plugin, have implemented it in a WP-based recruitment site and using GF to handle online job applications, hence the renaming of file e.g. cv_refnumber_surname_firstname.extension. Makes for easy filing on the server side of things.

    Posted 14 years ago on Monday December 21, 2009 | Permalink
  4. eventmedia,

    We don't have a hook specific for renaming the uploaded file, but with a little extra work, the post submission hook can do the trick.
    Following is the code snippet you will need. Place it in your theme's functions.php file.
    Also, you will need to replace the form id (line 6) and the field id's with the appropriate ones for your form (i.e. $entry[2] and $entry[1]). You can find the field id by inspecting your form's html and looking at the input name. It will be in the format name="input_ID" (i.e. name="input_2")

    add_action("gform_post_submission", "rename_file", 10, 2);
    function rename_file($entry, $form){
    
        //ignore requests from forms other than ID 18
        if($form["id"] != 18)
            return;
    
        $upload_info = wp_upload_dir();
    
        //original name/path
        $original_file_url = $entry[2];
        $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).
        //NOTE: Change this line to create the filename you would like to have
        $new_name = $entry["id"] . "_" . $entry[1];
    
        //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));
        }
    }
    Posted 14 years ago on Wednesday December 23, 2009 | Permalink