Skip to content

Instantly share code, notes, and snippets.

@Oleksandr-Moik
Created November 20, 2024 03:03
Show Gist options
  • Save Oleksandr-Moik/6000da55cfa762560b406d9e856eb7ed to your computer and use it in GitHub Desktop.
Save Oleksandr-Moik/6000da55cfa762560b406d9e856eb7ed to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Facades\File;
function generateUniquePathName(string $filePath): string
{
$path = File::dirname($filePath);
$name = File::name($filePath);
$extension = File::extension($filePath);
$suffix = '';
$counter = 1;
// $this->disk = Storage::disk('local'); // for example
while ($this->disk->exists("{$path}/{$name}{$suffix}.{$extension}")) {
$format = '-%s'; // any - ' (%s)', '-%s'
$suffix = sprintf($format, $counter);
++$counter;
}
return "{$path}/{$name}{$suffix}.{$extension}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment