Created
November 20, 2024 03:03
-
-
Save Oleksandr-Moik/6000da55cfa762560b406d9e856eb7ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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