dropbox-php-sdk icon indicating copy to clipboard operation
dropbox-php-sdk copied to clipboard

Check if file exists

Open daslicht opened this issue 7 years ago • 1 comments

Hi, is using Search the correct method to check if a file exists ?

~ Marc

daslicht avatar Aug 10 '18 13:08 daslicht

The "best" method I've found so far is to try to get the file's metadata, which will throw an exception if the file does not exist, but of course there are more reasons why an exception may be thrown so I check the code of the exception for the 'not found' code. This was achieved by trial and error so I'm not convinced it's the best way.

      try {
        $meta = $dropbox->getMetadata($filename);
      } catch (Exception $ex) {
        if ($ex->getCode() == 409) {
          // .. file does not exist
        } else {
          throw $ex; // re-throw any other exception
        }
      }

Yes, it's horrible :)

neekfenwick avatar Jul 07 '20 02:07 neekfenwick