Search code examples
phpdrupalfileapi

After uploading a file to Drupal 7 via a form, how to get the file's path?


I have a form that uploads a file to my Drupal installation. I want to store the path to that file in a table. How do I get the path to the recently uploaded file? I tried

$f = file_load($form_state['values']['field_file']);
$f->uri;

But that's not working. Any clues?


Solution

  • $f = file_load($form_state['values']['field_file']);
    $url = file_create_url($f->uri);
    

    The URI is the public:// private:// etc which Drupal uses internally. To convert it use file_create_url(); Ideally you should still store the URI and then use the file_create_url() when rendering.