Score:0

How to compare the original file and replacing file to upload prior to the action of physical uploading?

za flag

Exploiting the file_save_upload functions options, i would like to give a different treatment to the file on media replace.

https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/function/file_save_upload/8.6.x

  • if the new file to upload has the same uri than the existing, i would like to use FILE_EXISTS_REPLACE
  • if the new file to upload has a different URI, i would like to use FILE_EXISTS_RENAME

something like:

/**
 * Custom validate handler for media entity edit form submissions.
 */
function hook_validate($form, FormStateInterface $formState) {
  //if replacement file has same uri as original 
      $replacementFile = file_save_upload('replacement_file', $uploadValidators, $uploadDestination, 0, FileSystemInterface::EXISTS_RENAME);
  //else
       $replacementFile = file_save_upload('replacement_file', $uploadValidators, $uploadDestination, 0, FileSystemInterface::EXISTS_REPLACE);
}
cn flag
File Replace module replaces files, so it might give you an idea: https://www.drupal.org/project/file_replace
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.