How to save a file with the same name to Google Cloud Storage

I am creating a website with a lot of user uploaded image files. This way, over time, when a link might occur, there will be the same name images in the Google store. Even one user can upload an image of the same name at a time. So how can I solve this problem?

+3


source to share


1 answer


One thing you can do is add a timestamp to your file name on upload

eg.

$date = new DateTime();
$timeStamp = $date->getTimestamp();
$fileName = $fileName. "_" . $timeStamp;

      



And then when you allow the user to upload the file, read the filename and remove the last part from the underscore and give the link to the user with that parameter in the url with the original final name

$service = new Google_Service_Storage($client);
$objects = $service->objects->listObjects($bucketName);

// Assuming first item you are downloading
$objects = $objects['modelData']["items"][0];
$filename = substr($objects["name"], 0, strrchr($objects["name"],"_"));
$url .= "&response-content-disposition=attachment;%20filename=.$filename"

      

+3


source







All Articles