Finding the right path to a file in Drupal - Learning from the Drupal API

It quite the challenge to build a path to a file in the Drupal system without the help of the API. In particular, if you're building a module that needs to work regardless of where a site puts their files, you might end up lost in a rabbit hole of API functions that sound like they might do the trick, but don't.

The function you need in your tool belt to create well-formed Drupal file paths is file_create_path(). Here's an example of how the Filefield CCK module uses file_create_path:

$file = file_create_path($file);

Let's take a look at the code and see what's going on here:

function file_create_path($dest = 0) {
  $file_path = file_directory_path();
  if (!$dest) {
    return $file_path;
  // file_check_location() checks whether the destination is inside the Drupal files directory.
  if (file_check_location($dest, $file_path)) {
    return $dest;
  // check if the destination is instead inside the Drupal temporary files directory.
  else if (file_check_location($dest, file_directory_temp())) {
    return $dest;
  // Not found, try again with prefixed directory path.
  else if (file_check_location($file_path .'/'. $dest, $file_path)) {
    return $file_path .'/'. $dest;
  // File not found.
  return FALSE;

The first line calls file_directory_path, which just grabs the value set on the admin/settings/file-system page of your site. Next, it runs through a series of checks to first insure that that the path hasn't already been appended with the file system directory, next to see if the file isn't stuck in the temporary file directory, and finally adds the system path to the file and returns the full path if the file exists.

One thing to note is that once you have your full path, you shouldn't have to add anything else to use it. file_create_path should work on it's own to give you the file path you need.

I've used this function when I have a file that I know will always be in the file directory, but can't be sure where that directory will be. It took me way too long to dig this information up. There are a couple of other useful file-related API calls that I discovered along the way, including:

  • file_create_url - Creates a link to download a file
  • file_check_path - Insures that the directory that contains the file is writable by Drupal
Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:

Contact us

Below are the supported keyboard shortcuts for our video player.

Start / stop video
Go to next video
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
Jump to a percentage (2 is 20%, for example)
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
1 month
6 months
10% off
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.