Skip to main content
$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), ); function file_validate_name(stdClass $file) { $errors = array();
 //generate file name $new_filename = convert_filename_to_filename_date($file->filename); $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), ); function file_validate_name(stdClass $file) { $errors = array(); //generate file name $new_filename = convert_filename_to_filename_date($file->filename); $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), ); function file_validate_name(stdClass $file) { $errors = array();
 //generate file name $new_filename = convert_filename_to_filename_date($file->filename); $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), ); function file_validate_name(stdClass $file) { $errors = array(); //generate file name $new_filename = convert_filename_to_filename_date($file->filename); $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
added 1 characters in body
Source Link
amard
  • 21
  • 3

You can rename file on upload context through validator functions

here is the edited code

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

function file_validate_name(stdClass $file) { $errors = array();
 //generate file name $new_filename = convert_filename_to_filename_date($file->filename);  $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 

You can rename file on upload context through validator functions

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

 $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 

You can rename file on upload context through validator functions

here is the edited code

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), ); function file_validate_name(stdClass $file) { $errors = array();
 //generate file name $new_filename = convert_filename_to_filename_date($file->filename);  $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
added 1 characters in body
Source Link
amard
  • 21
  • 3

code>

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

 $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
 $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 

code>

$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

 $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
$form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => 'public://', '#upload_validators'=> array('file_validate_name' => array()), );

function file_validate_name(stdClass $file){ //generate file name $new_filename = convert_filename($file->filename);

 $file->filename = $new_filename; //changing file location $destination_dir = "public://file_location/"; //check if file exists. if exists rename it append incremental number until the filename is unique $file->destination = file_destination($destination_dir.$file->filename, FILE_EXISTS_RENAME); // If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and // there's an existing file so we need to bail. if ($file->destination === FALSE) { $errors[] = t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $file->source, '%directory' => $destination_dir)); } return $errors; } 
Source Link
amard
  • 21
  • 3
Loading