I'm using Drupal 7 and I have a content type, Picture, which includes an image field. Users use this to add image to the site.

Sometimes, however, there is no data on the server. For example, if I copy the database from my production to my testing server, the image files will be registered in the db but they don't actually exist on the testing server, so the pictures cannot be displayed.

I would like to create a view that shows these images-- the data is in the DB, but the file does not actually exist.

I created a view for my content type Picture and added the following Filter Criteria

  • Image: fid- empty
  • OR File size- empty (Through File Usage: File relationship)

However, this does not show pics that exist in the DB but not in the filesystem.

Is there a way to do this in views, or is there no way to test if the file actually exists?


It's never a good idea to put PHP code in the database, period. You should consider using a custom views handler. It's a somewhat involved process, but worth it in the end because your code can be version controlled, and you won't wind up with mysterious errors that are a result of PHP code being executed from the DB. There are a lot of great tutorials out there on how to do this, here's one that I used with great success - https://kalamuna.atlassian.net/wiki/spaces/KALA/pages/20512879/Custom+Views+Field+Handler.

Your custom handler in this case could return either the image, or a placeholder if none was found. You'll have full control over the query logic and what is returned. And you won't make a future developer miserable who inherits your codebase and can't find some potentially deprecated or no longer needed PHP code.


One way of going about this is using Views PHP. Add your file path as a field and exclude it from display. Add a Global: PHP filter with return file_exists($row->uri); as your filter code. This should return TRUE for files that do exist, and will remove those from the results, showing only files in your database that do not exist. Your current filter criteria are only checking the file_managed table in the database and are not going to be aware if the file exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.