KerrM
KerrM

Reputation: 5240

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?

Upvotes: 5

Views: 6237

Answers (2)

Ben Swinburne
Ben Swinburne

Reputation: 26497

$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.

Upvotes: 11

fonini
fonini

Reputation: 3351

Try to debug the $f object and see if some error happens:

$f = file_load($form_state['values']['field_file']);
echo '<pre>';
print_r($f);

Upvotes: 0

Related Questions