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.

File upload location and name

  1. I've seen several posts ask this, but no simple answer. here is how you can rename files upon upload

    ex: we want to change the files for form 9

    add_filter("gform_upload_path", "custom_upload_path", 10, 2); //this is a gf filter which changes the upload path
    function custom_upload_path($path_info, $form_id){
    	if($form_id==9){
    		$path_info["path"] = "/home/youruser/public_html/wp-content/uploads/customfolder/";
    		$path_info["url"] = "http://example.com/wp-content/uploads/customfolder/";
    	}
    	return $path_info;
    }
    
    add_action("gform_after_submission_9", "custom_after_submission_9", 10, 2); //this is a gf filter which lets us get an entry after it is done.  we will use this to automatically rename files (only form 9 in the example)
    function custom_after_submission_9($entry, $form){
        foreach($form['fields'] as $key => $field){  //this will get all the fields that are a fileupload type
    	    if($field['type'] == 'fileupload')
    	    	$keys[] = $field['id'];
        }
        foreach($keys as $value){  //this will save the url info for all the fields which were submitted
        	$pathinfo = pathinfo($entry[$value]);
        	if(!empty($pathinfo['extension'])){
    	    	$oldurls[$value] = $pathinfo;
    	    	$pathinfo['filename'] = $entry['id'] . '_' . $form['id'] . '_' . $value;  //we are going to make the files look something like this entryid_formid_fieldid... so you may have something like 323_9_12 for the filename.   this system ensures all filenames are unique and sorted.  you can make the name anything you like.  remember it is the filename only (no path and no extension)
    	    	$newurls[$value] = $pathinfo;
        	}
        }
        $uploadinfo = custom_upload_path('', 9); //this will get the upload path from our custom filter
        foreach($newurls as $key => $value){
        	$oldpath = $uploadinfo['path'].$oldurls[$key]['filename'].'.'.$oldurls[$key]['extension'];
        	$newpath = $uploadinfo['path'].$newurls[$key]['filename'].'.'.$newurls[$key]['extension'];
        	$oldurl = $uploadinfo['url'].$oldurls[$key]['filename'].'.'.$oldurls[$key]['extension'];
        	$newurl = $uploadinfo['url'].$newurls[$key]['filename'].'.'.$newurls[$key]['extension'];
        	$is_success = rename($oldpath,$newpath); //this renames the file
        	if($is_success && !empty($is_success)){
    	    	global $wpdb;
    	    	$wpdb->update(RGFormsModel::get_lead_details_table_name(), array("value" => $newurl), array("lead_id" => $entry["id"], "value" => $oldurl)); //this updates wordpress
        	}
        }
        return;
    }
    Posted 12 years ago on Tuesday September 18, 2012 | Permalink
  2. David Peralty

    Very awesome, thanks for doing this!

    Posted 12 years ago on Tuesday September 18, 2012 | Permalink

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