Type to search...

Guide for easy image replacing

Shardimage won’t let you upload an image into a cloud with an already existing public ID, the process will result in an error. However replacing an image with the same public ID can be a real demand, especially if the public ID has meaning and not randomized. From an SEO perspective, if you want to help Google to understand what your image is about, you definitely should use a public ID which describes the content of your image.

For example, if your image shows a product, like a cheap brown hat, the public ID rather should be cheap-brown-hat.jpg instead of DSC365126436.jpg.

Allowing override

A not too efficient solution: before uploading the image we check if the image exists or not, and if the response is true, we delete the old image and then upload the new one. It’s easy to see it’s already two requests to the API in the best case; also, we need to add more logic and code which can lead unwanted errors and bugs. It’s possible that a web user is referring the image after deleting the old one but before uploading the new one. In this case, the user get a 404 response.

The best solution is to use the allowOverride HTTP request parameter. After turning on, the upload won’t throw error in case of already existing public ID, the old image will be deleted and the new will be uploaded automatically. For more information about upload HTTP request parameters, please check our documentation here »


The easiest way is to use the UploadBuilder class. For more details about the UploadBuilder, check out our documentation here »

use shardimage\shardimagephp\auth\Client;
use shardimage\shardimagephp\builders\UploadBuilder;
$client = new Client([
    'apiKey' => '<apiKey>',
    'apiSecret' => '<apiSecret>',
    'imageSecret' => '<imageSecret>',
    'cloudId' => '<cloudId>',
$fileResource = fopen("<path_to_file>", 'r');
$builder = (new UploadBuilder())->withPrefix('SDK-TEST-')
    ->allowOverride() // override turning on
$params = $builder->build();
$result = $client->getUploadService()->upload($params);
Table of contents