I'm working with the Rackspace PHP API, where I've got a requirement to check the file, if it exists then do something and if not then do something.
try {
$file = $container->getObject($end_element);
$fileExists = TRUE;
}
catch(Exception $e) {
$fileExists = FALSE;
}
if ($fileExists) {
// File is their, it needs to be rewrite/overwrite
$file->setContent(fopen('sites/default/files/rackspace/' . $end_element, 'r+'));
$file->update();
// I'm getting this http://docs.rackspace.com/sdks/api/php/class-OpenCloud.ObjectStore.Resource.DataObject.html which I printted print_r($file->update());
}
else {
// New file just to upload
$container->uploadObject($end_element, fopen('sites/default/files/rackspace/' . $end_element, 'r+'), array());
}