22

I can't find a Drupal API function in D7 that tells me if an (unmanaged) file exists. D6 had a file_check_location but not for 7.

How to check if (unmanaged) file exists?

4 Answers 4

44

You could just use the old bog standard PHP function file_exists() if I understand you right:

$uri = 'public://images/an-image.jpg';
if (file_exists($uri)) {
  // Do something
}

This also works for normal (absolute) paths as well, e.g.:

$path = '/var/www/drupal/sites/default/files/images/an-image.jpg';
if (file_exists($path)) {
  // Do something
}
6
  • Clive to the rescue again, thanks! Can I hire you? ;-)
    – uwe
    Commented Mar 2, 2012 at 19:55
  • 6
    @MotoTribe Depends how much you're offering ;)
    – Clive
    Commented Mar 2, 2012 at 21:30
  • Why does this work? I just tested this with plain PHP and a stream handle thrown into file_exists() and it does not work. Any explanation?
    – kaiser
    Commented May 31, 2016 at 13:36
  • 1
    Because the streams have been properly registered by Drupal @kaiser. See php.net/manual/en/wrappers.php
    – Clive
    Commented May 31, 2016 at 13:39
  • @Clive I hoped you can point me to the source in Drupal. The rest I know and can not get working in a plain PHP test, hence I asked. Edit: nvm, found them
    – kaiser
    Commented May 31, 2016 at 14:03
7

If you want a Drupal way to do this, you can in Drupal 8/9/10 using the file_system service.

/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$file_exists_error = $file_system->getDestinationFilename($file_uri, \Drupal\Core\File\FileSystemInterface::EXISTS_ERROR);

If the file does not exist in the file system $file_exists_error will return the file path, otherwise it will return false, denoting there was no error.

@see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21File%21FileSystemInterface.php/function/FileSystemInterface%3A%3AgetDestinationFilename/9.3.x

1
  • In Drupal 10 you can just call file_exists on a public://path/to/file uri and it will return correctly since Drupal registers the stream wrappers at bootstrap
    – anoopjohn
    Commented Dec 2, 2023 at 8:10
4

I don't know what your specific use case is, but you may not need to check to see if the file exists.

The functions file_unmanaged_copy, file_unmanaged_move, file_unmanaged_delete, file_unmanaged_delete_recursive, and file_unmanaged_save_data check to see if the file exists and return false if it does not.

You can find the source code in drupal/includes/file.inc and have a look.

4

You should use the drupal function:

file_destination($uri, FILE_EXISTS_ERROR)

and check

if (!file_destination($uri, FILE_EXISTS_ERROR)) {
  // The file exist
  // Do something
} 
1
  • 1
    It will work as file_destination() uses file_exists() function
    – milkovsky
    Commented Mar 31, 2014 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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