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; }