Skip to content

Instantly share code, notes, and snippets.

@ycombinator
Created March 6, 2014 17:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ycombinator/9395033 to your computer and use it in GitHub Desktop.
Save ycombinator/9395033 to your computer and use it in GitHub Desktop.
How to check if a file exists in a Rackspace Cloud Files container
<?php
// Assumption: $container is an instance of OpenCloud\ObjectStore\Resource\Container
$filename = 'mugshot2.png';
try {
// Try to retrieve the file, but only its headers to save
// bandwidth and time.
$container->getPartialObject($filename);
// If we get this far, then the file exists
$fileExists = true;
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
// If a 404 was returned, then the file doesn't exist
$fileExists = ( $e->getResponse()->getStatusCode() != 404 );
}
if ($fileExists) {
// skip the file
} else {
// don't skip the file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment