I've a module that in the .install creates a table. In that table I have to save the information of one image, that's imagen_leyenda.

enter image description here

I obtain that image from a form, that I have created in my custom module. To save that image, I have the next code:

function guardar_leyenda(&$form_state,$values){
        $icono = $form_state['step_information'][1]['stored_values']['icono'];
        $query = db_insert('leyenda')->fields(array('imagen_leyenda','texto_grande','fecha_inicio','fecha_fin'));
        $query->values(array($icono,$texto2,date('Y-m-d H:i:s'),NULL));

Now, what I need is to obtain the url of that image, but as you can see in the table, the id of the image is store, and what I need is the name of the image.

Is there any way to do that? Or maybe, have I to change something of the previous code?

Thank you very much!


This piece of code is the form icono

function form_leyenda_editar(&$form, &$form_state){

    $form['icono'] = array(
        '#title' => t('Icono'),
        '#type' => 'managed_file',
        '#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
        '#upload_location' => 'public://',
        '#progress_indicator' => 'throbber',
        '#status' => FILE_STATUS_PERMANENT,
        '#progress_message' => 'One moment while we save your file...',

So, after when the user upload an image, drupal generates the id stored in imagen_leyenda.

  • Hi, what's the stored id? A File ID generated by Drupal? Commented Dec 27, 2014 at 16:05
  • Yes, I think. Drupal generates that id.
    – more770
    Commented Dec 27, 2014 at 16:29
  • What's the id ? The 1 after $form_state['step_information'] ?
    – rtome
    Commented Dec 27, 2014 at 16:37
  • that $form_state is the form "icono", I have edited my question.
    – more770
    Commented Dec 27, 2014 at 16:47

1 Answer 1


Since you store the file ID, you can fetch the value from the database and obtain the image URL like this:

$file = file_load($file_id);
$image_url = file_create_url($file->uri);

Also, you can get the uploaded image with custom dimensions with image_style_url($style, $image_url). You can configure styles here admin/config/media/image-styles.

  • I think that is a very logical solution, but it doesn't work. $file is false... so I don't realy know what is that number that drupal stores in the table... any other idea?
    – more770
    Commented Dec 27, 2014 at 16:50
  • Hmm, you should check the file_managed table to be sure it's not a file ID. Maybe your call to file_load() is made too early but I don't think that's the issue. Commented Dec 27, 2014 at 16:54
  • OK. I have seen that is not the file id. Id 342 and 343 doesn't exit in that table. So maybe, I have to change my question and ask how to save in the correct way the images programatically.
    – more770
    Commented Dec 27, 2014 at 17:00
  • You can use file_save_upload to save your file. There's a lot of doc on the interweb ;) Commented Dec 27, 2014 at 17:20
  • Thank you for your help. You answer help me to find the correct solution. What I have done is save the image in file_managed table as here drupal.org/node/2313337, and then use the code that you have said. Thank you again
    – more770
    Commented Dec 27, 2014 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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