Skip to main content

How to set the Private file path in Drupal using the following steps:

  • Create a private folder in the web root
  • Add a .htaccess file to the private folder
  • Update the settings.php file

 

Create a private folder in the web root

Open a terminal window and navigate to the web root directory of your Drupal site. In your web root add your private folder.  In this instance I'll create a folder named 'prvt-media'.  Use the mkdir command to create a new folder named prvt-media

mkdir prvt-media

Once created, check the permissions.  To view the permissions, use

ls -l

Response will be similar to 

drwxr-xr-x 39 www-data psacln    4096 Apr 14  2022 libraries
drwxr-xr-x  4 www-data psacln    4096 Apr 16 11:10 modules
drwxr-xr-x  2 www-data psacln    4096 Aug  7 10:45 prvt-media
-rw-r--r--  1 www-data psacln    1706 Apr 16 11:10 robots.txt
drwxr-xr-x  3 www-data psacln    4096 Apr 16 11:10 sites
drwxr-xr-x  4 www-data psacln    4096 Apr 16 11:10 themes

If the permission isn't correct, you'll need to adjust using chown command, such as

chown -R www-data:psacln prvt-media

 

Add a .htaccess file to the private folder

Drupal requires a htaccess file is present.  Create a htaccess file, for me I used vim

vim .htaccess

Add the following contents to the file


# Turn off all options we don't need.
Options None
Options +FollowSymLinks

Deny from all

# Set the catch-all handler to prevent scripts from being executed.
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
<Files *>
  # Override the handler again if we're run later in the evaluation list.
  SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003
</Files>

# If we know how to do it safely, disable the PHP engine entirely.
<IfModule mod_php5.c>
  php_flag engine off
</IfModule>

 

Update the settings.php file

Now the pre-work has been undertaken, we can update the settings.php file.  Edit your settings.php file and scroll or find the private path text


/**
 * Private file path:
 *
 * A local file system path where private files will be stored. This directory
 * must be absolute, outside of the Drupal installation directory and not
 * accessible over the web.
 *
 * Note: Caches need to be cleared when this value is changed to make the
 * private:// stream wrapper available to the system.
 *
 * See https://www.drupal.org/documentation/modules/file for more information
 * about securing private files.
 */
# $settings['file_private_path'] = '';

 

Uncomment the line and change the value of '' to the path of the private folder you created in step 1. For example, if the path to your private folder is /var/www/html/mysite/private, you would change the line to:

$settings['file_private_path'] = 'prvt-media';

Save the settings.php file.

 

Clear the Drupal cache

I'll clear the cache using Drush and command line

drush cr

Or using the admin interface

  1. Go to the Drupal admin dashboard
  2. Click on the Configuration tab
  3. Click on the Performance tab
  4. Click on the Clear all caches button

 

Once you have completed these steps, the private file path for your Drupal site will be set.

Here are some additional things to keep in mind:

  • The private file path must be an absolute path
  • The private file path must be outside of the Drupal installation directory
  • The private file path must not be accessible over the web
  • If you follow these guidelines, your private files will be securely stored and accessible only to authorized users.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...