I want an alternative for file_directory_path() deprecated in Drupal 7. I got an alternative with file_default_schema().

What is the difference between DRUPAL_ROOT and file_directory_path() or DRUPAL_ROOT and file_default_schema()?


DRUPAL_ROOT is a root folder of Drupal's installation. file_directory_path() returns a path to directory meant for uploaded files, default DRUPAL_ROOT/sites/example.com/files (replace example.com with your site or default). If you will replace it with hardcoded path, your modules will break on sites configured to store files in other directory. So this is not the way.

If you need it for forms, use new managed_file form element. It takes care. No need to provide path.

For reading file, if you know it's ID it's easy again. Load it and access it's properties, like that:

file_load($fid)->uri; // get URI to display it

If you don't want to use managed files (you should, but no one can force you), use:

$dir = file_default_scheme() . '://' . $any_subdirectory_you_want;

This will create directory string Drupal functions understand. For example, you can use it as $destination in file_save_upload($source, $validators, $destination, $replace) - again, no need for hard-coding actual path or using DRUPAL_ROOT on your own.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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