Change upload path based on field value

Hi,

i was using this function and it was working well.

add_filter( 'gform_upload_path_6', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id) {

	$entry = GFFormsModel::get_current_lead();

	// Vérifier si l'entrée et le champ désiré existent
	if ( !empty($entry) && !empty($entry['1']) ) {
		$user_dir = sanitize_file_name(rgar( $entry, '1' ));
	}
	
	// Vérifier si l'entrée et le champ désiré existent
	if ( !empty($entry) && !empty($entry['3']) ) {
		$doc_type_dir = rgar( $entry, '3' );
	}
	
	// Construire le nouveau chemin d'upload
	$new_path = WP_CONTENT_DIR.'/uploads/custom-dir/'.$user_dir.'/'.$doc_type_dir;
	
	$path_info['path'] = $new_path;
	$path_info['url'] = 'http://www.domain.com/wp-content/uploads/custom-dir/'.$user_dir.'/'.$doc_type_dir;

   return $path_info;
}

But now i have a ‘error 500’ and it com from this line
$entry = GFFormsModel::get_current_lead();

I can’t find an alternative to this.

How can i do to use fields values to change ulpoad path.

Thanks in advance

What is the complete error message you receive when the 500 error is triggered?

No error is diplayed. I remember in log that was memory issue.
Sorry i finally find a solution by modifying and using gravity wiz rename plugin but this code was so simple that i want it to work and understand what is the problem.

Thanks in advance for your reply.

The changelog for 2.8.6 does include the following line:

API: Updated the file upload field value in the draft entry from GFFormsModel::create_lead() to contain a JSON encoded array of file details instead of the incorrect file URL.

GFFormsModel::get_current_lead(), which your code uses, calls GFFormsModel::create_lead() if there isn’t a cached entry available.

So if field ID 1 in your code is a file upload field, you’ll need to update your code to account for that change. Here’s an example:

$file_field_value = rgar( $entry, 1 );
if ( empty( $file_field_value ) ) {
	return $path_info;
}

$files = json_decode( $file_field_value, true );
if ( empty( $files ) ) {
	return $path_info;
}

That will give you an array of arrays. Each array contains the details about a file. This is the same for both the standard file upload field and the multi-file enabled field.

You can then access the details of the first file like so:

$tmp_path      = rgars( $files, '0/tmp_path' );
$tmp_url       = rgars( $files, '0/tmp_url' );
$tmp_name      = rgars( $files, '0/tmp_name' );
$uploaded_name = rgars( $files, '0/uploaded_name' );

To access the details of other files, when multi-file is enabled, replace 0 with the file index.

Thank you very much for this complete response. I will try this.

Finaly I have no time try deeply. Maybe your post could help other people. Thank you.