Optimizing file paths: Organizing files in subfolders

Handling large files

If you're starting a new website, and don't have many files uploaded yet, you can Proactively organize your files in subfolders. If you've got a website that has already been in use for some time, the following information may be more helpful to you.

Website performance can severely degrade when too many files are added and available to the system in a single directory without any subdirectory structure. For example, on Acquia Cloud, we have found that over 2500 files in any single directory in the files structure can seriously impact a server's performance and potentially its stability. To avoid this problem, you should split the files into subfolders that are identified by some characteristic of the files. You can use dates as tokens for these directories. The File (Field) Paths and File Entity Paths modules allow you to use node tokens to select the directory where the images or other uploaded files should reside. Simply select the paths for files attached either through file fields or by way of the Drupal core upload module.

An unruly directory is a common problem that one realizes only when the file directory becomes too large and has too many files on any single level of a directory, and not before then. Then it's necessary to relocate not only new files, but also existing ones. To help new clients with this problem, Acquia developed Drush scripts for both Drupal 7 and Drupal 6 that moves uploaded files into a subfolder structure. Regardless of which version you use, you can name the script migration_filepath.drush.inc.

Drupal 7

The provided script processes a target files folder, moves the files directly into that folder (it is not recursively looking into subfolders), and then moves them to a target location. There, it establishes a [year]/[month]/[day] directory structure for the files; the date specifies when the file was uploaded.

To start the script, simply issue the following Drush command from the active website's docroot:

drush migration_filepath

There are two optional parameters, --source and --target, in case the source and target are not the default files folder, but, for example, a subdirectory underneath.

The script also updates the database; after the file migration, all content should correctly reference the new file locations.

By default, the script moves only certain file types, but you can easily change this by editing the $extensions variable. The script is case sensitive, so .JPG and .jpg files are treated separately.

After running the script, be sure that the file path is using the token structure; otherwise after this cleanup, the website will continue to place new files into a flat structure, leading again to a problem after it grows too large.

The runtime depends on the number of files in the folder and the speed of the filesystem. For a comparison, around 50,000 files were successfully migrated in about two minutes on Acquia Cloud.

Download this script

Drupal 6

To use this Drush script, create or download the Drush script file and put it inside your server's ~/.drush/ directory. You can define all imagefields to use the same pattern. Then, execute something like the following at the command line:

drush migration_changefields_filepath

After this is done, you can effectively move existing images to a new location by entering something like the following from the command line:

drush migration_filepath

Download this script

Contact supportStill need assistance? Contact Acquia Support