Score:0

how to get file id

ph flag

I have this webform submission hook and i want to add a file to it when its submitted. I know how to alter the webform submission but now i have an issue that i want to load a file but i don't know the id, only it's url:

Url: /sites/default/files/assets/filename.pdf

i have tried:

   $file_name = Drupal::service('file_system')->basename($filename);

   //$file_name gives me the correct name


  $target_file = Drupal::entityTypeManager()->getStorage('file')
                  ->loadByProperties(['filename' => $file_name]);

  //$target_file is an empty array

How can i get the file id by its url

Any help is welcome :D

apaderno avatar
us flag
The code you are using seems to assume that for every file on the file system there is a file entity (which isn't true). In the case there is a file entity for that file, it's probably stored using its full filename.
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.