Same file_temporary_path for localhost and production server? Each time I move one of my Drupal website from localhost to production server or vice-versa, I have to edit my settings.php to update the path of the temporary file directory:

# $conf['file_temporary_path'] = '/home5/dawnings/public_html/fw2/tempdir';
$conf['file_temporary_path'] = 'c:/Temp/Drupal/tempdir';


Is there a way to specify a unique relative path to this temporary file directory once for all and which would work both for localhost and production server? The objective is to avoid having to update my settings.php anymore.
There is more than 1 way to accomplish something like this. I am not aware of a simple "1 line universal fix" unless you try a relative path, but that assumes some consistancy between your servers which clearly by your directory paths you dont have.

Option 1:

At the bottom of you settings.php put something like:

$local_settings_conf = dirname(__FILE__) . '/settings.local.php';
if (file_exists($local_settings_conf)) { include $local_settings_conf; }


I do this programmatically in BASH with like:

#!/bin/bash

# I actually install drupal via the command line with a custom install
# profile and append on the tidbit to settings.php programmatically like
# so ...

cd $drupal_www_dir
drush site-install MY_INSTALL_PROFILE \
  --locale=en \
  --db-url="mysql://dev_user:dev@localhost:3306/demo_drupal" \
  --account-name=admin \
  --account-pass='admin' \
  --acount-mail=foo@test.com \
  --site-name=demo --verbose -y


#
# Now that drupal is built we modify the settings.php file with additional db
# and other custom configuration values by appending a child seetings file.
#
# This is in concept similar to an Apache vhost directive file.
#
echo "altering settings.php to be aware of our 3rd party remote databases."
sudo chmod -Rv a+rw "${drupal_www_dir}/sites/default"
read -r -d '' PHPCODE &lt;&lt;'EOF'
/*
 * For our project we provide our own localized configuration schemes.
 */
$local_settings_conf = dirname(__FILE__) . '/settings.local.php';
if (file_exists($local_settings_conf)) { include $local_settings_conf; }

EOF
echo "${PHPCODE}" &gt;&gt; "${WWW}/sites/default/settings.php"


Then for each server you keep a version of this file. Or in this file make an If-Else PHP block that stores the correct $conf value based on whether this configuration file is $local vs $dev or $prod. So basically you can move all your custom settings.php stuff that arent 100% stock drupal into a special appended configuration file ... this way the stock drupal settings.php stays as it is and all your custom stuff can go into its own file as-needed.

Option 2:

Use something like the Environment module and in PHP set the value for the file_temporay_path with variable_set as that is what is being called by file_directory_temp().

Those are the two options I know of that aren't brain surgery ...

EDIT 09/26/2014 by author


You can also use Drush on each server to run: drush vset file_temporary_path "to/some/path"
If you're comfortable with virtualhost settings you can also always create a aliased directory in your webserver configuration (pushing the problem up 1 level in the production stack (out of PHP))