Created
March 6, 2014 17:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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