Solving missing files issue when setting up a drupal site without files folder

| | 2 min read

When you try to set up a local development environment from a drupal dump which includes just the code base and the database without the Drupal files folder you will run into the issue of missing files. Drupal would think that it still has all the files in the Drupal files folder and would create links / process images as usual. This would however lead to a lot of unexpected errors in a lot of unexpected places. If you really do not care about the content in these files and want to recreate the missing files here is how you do this.

First copy over index.php and create test.php. Replace menu_execute_active_handler(); with custom code to run the following query and output the list of files in the system.

SELECT REPLACE(uri, 'public:/', '') AS path FROM file_managed WHERE uri LIKE ('public://%') ORDER BY filemime ASC

Create a folder test inside the webroot. Save the list of files as files.txt inside this folder.

Now copy over the following code into a bash script inside the same folder

#!/bin/bash

while read line
  do
    #echo $line;
    path="../sites/default/files$line"
    if [ ! -f "$path" ]; then
      #echo "File does not exist - $path"
      extn=`echo $line | sed 's/^.*\.//'`
      new=""
      case "$extn" in
        jpg|JPG|jpeg|JPEG)
          new=test.jpg
          ;;
        png|PNG)
          new=test.png
          ;;
        doc|DOC|docx|DOCX)
          new=test.doc
          ;;
        pdf|PDF)
          new=test.pdf
          ;;
        xls|XLS|xlsx|XLSX)
          new=test.xls
          ;;
        ppt)
          new=test.ppt
          ;;
        txt)
          new=test.txt
          ;;
        zip)
          new=test.zip
          ;;
        svg)
          new=test.svg
          ;;
        mp4)
          ;;
        *)
          echo "$extn"
          ;;
      esac
    fi
    if [ ! -z "$new" ]; then
      cp "$new" "$path"
      chown apache:apache "$path"
      echo "Created $path"
    fi
    #echo $path;
  done < "./files.txt"

The code is pretty self-explanatory for a programmer. If you are not one, we can help you solve this problem :-).

Happy Hacking.