How to Organise and Maintain Uploaded Files in Mautic

4 min read

As an administrator of a Mautic system, managing uploaded files is crucial to maintaining a clean, secure, and efficient environment. Uploaded files—whether they are documents, images, or other media—can quickly accumulate and consume disk space if left unchecked. This article provides detailed guidance on how to manage uploaded files effectively, ensuring your Mautic instance remains organised and performant.

Understanding File Uploads in Mautic

When users submit forms with file upload fields, Mautic stores the uploaded files in a directory specified by the upload_dir setting in your configuration. By default, this directory is located under /path/to/mautic/media/files. Files are further organised into subdirectories based on the form ID and submission ID (e.g., /form/3/45/example.pdf).

While this structure helps keep files logically organised, it is up to you as the admin to ensure that these files do not become unmanageable over time.

Step 1: Configure the Upload Directory Correctly

The first step in managing uploaded files is ensuring that the upload_dir setting is correctly configured. Misconfigured paths can lead to errors, such as open_basedir restrictions or inaccessible files.

  1. Open your Mautic configuration file (local.php or config.php) and locate the upload_dir setting:
    'upload_dir' => '/home/mautic/webapps/go100/public/media/files',
    
  2. Ensure the specified path exists and is writable by the web server user (e.g., www-data):
    mkdir -p /home/mautic/webapps/go100/public/media/files
    chown -R www-data:www-data /home/mautic/webapps/go100/public/media/files
    chmod -R 755 /home/mautic/webapps/go100/public/media/files
    

If you need to change the upload directory, update the upload_dir setting and restart your web server.

Step 2: Restrict File Types and Sizes

To prevent users from uploading excessively large files or unsupported formats, configure your forms to restrict file types and sizes:

  1. Navigate to Forms > Edit the form > Fields.
  2. For the file upload field, specify allowed file extensions (e.g., pdf,docx,jpg) and set a maximum file size (e.g., 5 MB).

Example

  • Allowed file types: pdf,docx,jpg,png
  • Maximum file size: 5 MB

This ensures that only relevant files are uploaded, reducing clutter and potential security risks.

Step 3: Periodically Clean Up Old Files

Over time, uploaded files can accumulate and consume disk space. To prevent this, periodically clean up old or unused files. Below are two methods for doing so:

a. Manual Clean-up

Manually delete files from the upload directory using the command line or a file manager:

rm /home/mautic/webapps/go100/public/media/files/form/3/45/example.pdf

Alternatively, use a file manager (e.g., via FTP or hosting control panel) to browse and delete files.

b. Automate Clean-up with a Cron Job

Set up a cron job to automatically delete files older than a certain number of days. For example, the following script deletes files older than 30 days:

find /home/mautic/webapps/go100/public/media/files -type f -mtime +30 -exec rm -f {} \;

Add this script to your server’s cron jobs:

  1. Open the crontab editor:
    crontab -e
    
  2. Add the following line to run the script daily at midnight:
    0 0 * * * find /home/mautic/webapps/go100/public/media/files -type f -mtime +30 -exec rm -f {} \;
    

Step 4: Archive Important Files

For files that need to be retained but are no longer actively used, consider archiving them:

  1. Compress old files into .zip or .tar.gz archives:
    tar -czvf archived_files.tar.gz /home/mautic/webapps/go100/public/media/files/form/3
    
  2. Move the archive to a separate directory or external storage (e.g., cloud storage like AWS S3 or Google Drive).

Step 5: Monitor Disk Usage

Regularly monitor the disk space used by uploaded files to avoid running out of storage:

du -sh /home/mautic/webapps/go100/public/media/files

If disk usage is high, consider cleaning up old files or moving them to external storage.

Step 6: Educate Users

If your forms are publicly accessible, educate users on what types of files to upload and the importance of keeping uploads relevant. For example:

  • Add instructions in the form description: "Please upload only relevant documents (e.g., PDFs or images)."
  • Use validation rules to enforce file types and sizes.

Step 7: Backup Uploaded Files

Ensure that uploaded files are included in your backup strategy. If you ever need to restore Mautic, you’ll want to include the uploaded files to avoid broken links or missing data.

Further notes...

By following these best practices, you can effectively manage uploaded files in Mautic and prevent them from becoming unmanageable. Key takeaways include:

  • Configuring the upload_dir setting correctly.
  • Restricting file types and sizes in forms.
  • Periodically cleaning up old or unused files.
  • Archiving important files and monitoring disk usage.
  • Educating users and including uploaded files in backups.

With these steps in place, your Mautic instance will remain organised & secure. If you have any questions or need further assistance, consult your server administrator or refer to Mautic’s official documentation.