Drupal 7: create random filename

It’s very easy to create a random filename in Drupal. Just re-use the built-in Drupal user_password function:


In my case today, I wanted to copy a file from the private:// area to the public:// area because flex was not picking up the xml file I wanted. It’s important to me that I obscure the filename so this is what I do in hook_file_insert()

// XML files need to saved in public area, so that flex will work.
$random = user_password(20);
$dest = 'public://' . $random;
file_copy($file, $dest, FILE_EXISTS_REPLACE);
Posted in drupal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: