I've a column in database containing the IDs of the files uploaded using a managed file form field.

How do I create a link to the uploaded file knowing its file ID?

EDIT : The obvious solution doesn't work for me , i'm using another table where i put my Fids .

this is the warning i got everytime : Notice: Trying to get property of non-object in Affichage_form() (line 188 of C:\wamp64\www\Gestion_tickets_licences\sites\all\modules\Recherche\Recherche.module). << 188 : $uri= $file->uri; >>

Solution : Thank you for you help . my solution for my problem is , Once I upload the file , I create the url. Instead of saving Fid , I save the url , It worked .For the download link , I used Alen syntax and it worked great .

  • just display it as a generic file (display settings in node or in view) – pinueve Jul 28 '18 at 15:12
  • Can you explain more i'm new with drupal – Aya Jul 28 '18 at 17:53

You can load file with id with file_load function and then you need to write <a href='file_path' download > file_name </a>

  • thank you but it didn't work for me , does the fid need to be registered in the fale_managed table ? because in my case I have a different table where I put fids of my uploaded files . – Aya Jul 30 '18 at 15:21
  • 1
    Yes, use Drupal's built-in file management system, it'll make your life a lot easier in the long term. – Alfred Armstrong Jul 30 '18 at 15:55

First, load the file using file_load(FILE_ID) and get URI like below:

$file = file_load(1);
$uri = $file->uri;

After that generate link using file_create_url();

$url = file_create_url($uri);

Get file name also:

$filename  = $file->filename;

And finally, create a link using l().

l(t($filename), $url);

and output will be like <a href="http://d7.local/sites/default/files/pictures/picture-6.png">picture-6.png</a>

  • I did this before but somehow the $uri is always empty , Notice: Trying to get property of non-object in Affichage_form() (line 188 ) which is $uri = $file->uri; – Aya Jul 30 '18 at 15:16
  • Can you update your question with code you have tried? – Ajay Reddy Jul 30 '18 at 16:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.