I'm writing an update function to set the file_private_path variable and then move files to the private location. This is what I have so far:

// Define path variables
$private_path = conf_path() . '/files/private';

// Set the private path variable and create the directory
variable_set('file_private_path', $private_path);
drupal_mkdir($private_path);
file_create_htaccess($private_path, TRUE);

// Find certain managed files
$file_query = db_select('file_managed', 'fm') 
  ->fields('fm', array('fid'))
...<snip>...
$file_result = $file_query->execute();

// Move all the files to the private file area
while($record = $file_result->fetchAssoc()) {
  $file = file_load($record['fid']);
  $file = file_move($file, 'private://');
}

But file_move fails because "because the destination private:// is invalid." If I split the process into two scripts, where I first set the variable, and then do the file_move, it works ok. So I guess Drupal needs to bootstrap again after file_private_path is set so that it knows where "private://" is. Is there something I can do to allow this all to happen in one step?

Thanks in advance for any tips.

Comments

jaypan’s picture

This code rests in index.php of a completely non-Drupal folder, correct?

Contact me to contract me for D7 -> D10/11 migrations.

knaffles’s picture

My intention is to either put the code in a hook_update_N function for one of my custom modules, or execute the script via drush.

jaypan’s picture

Your code makes no mention of webform at all, yet your error message mentions it.

You're not making this easy.

Contact me to contract me for D7 -> D10/11 migrations.

knaffles’s picture

Sorry...copy/pasted the wrong error. It should just say "the destination private:// is invalid."

jaypan’s picture

Well, you could theoretically try to split it into two hook_update_n() functions (ex: 7001 and 7002), though I'm not sure that would solve your problem.

Or you can also try adding this after setting the private path:

variable_set('menu_rebuild_needed', TRUE);

The above code is what is used after setting the file path on the admin page.

All speculation though.

Contact me to contract me for D7 -> D10/11 migrations.

knaffles’s picture

I ended up just including the update to the file_private_path variable in an hook_update_N function, and then just using drush to run a separate script to move the files around.