mattgorle / filename-suffixer
Ensures unique filenames by adding a numeric suffix
Installs: 666
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mattgorle/filename-suffixer
Requires
- php: ^7.2|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Ensures that filenames are unique by appending a suffix to the filename.
If a file with $filename does not already exist, then the original filename is offered instead of an altered one.
Examples
For a file named test_file.txt, FilenameSuffixer::suffix will return:
test_file.txtif the file does not existtest_file-1.txtif a file calledtest_file.txtalready existstest_file-2.txtif files calledtest_file.txtandtest_file-1.txtalready exist- etc...
Versioning
This project follows semantic versioning.
Installation
composer require mattgorle/filename-suffixer
Usage
use Gorle\FilenameSuffixer\FilenameSuffixer; $filename = 'your_file.jpg' $safeFilename = FilenameSuffixer::suffix($filename); // - your_file.jpg if the file does not already exist // - your_file-1.jpg if your_file.jpg already exists // - your_file-2.jpg if your_file-1.jpg already exists // - etc...
Customising behaviour
I have tried to select sensible defaults, but you can modify several parameters to suit your
individual use case by changing the value of the static properties on FilenameSuffixer.
You can customise the following:
| Option | Default Value | Property |
|---|---|---|
| File extension separator | . |
FilenameSuffixer::$extensionSeparator |
| Suffix separator | - |
FilenameSuffixxer::$suffixSeparator |
| Path separator | / |
FilenameSuffixer::$pathSeparator |
| Suffix starting number | 1 |
FilenameSuffixer::$suffixStartsAt |
Examples
use Gorle\FilenameSuffixer\FilenameSuffixer; FilenameSuffixer::$extensionSeparator = '_'; FilenameSuffixer::$pathSeparator = ':'; FilenameSuffixer::$suffixStartsAt = 31; FilenameSuffixer::$suffixSeparator = '_';
Resetting the suffixer after changing defaults
Help, I've changed loads of stuff and now I can't find my way back!!! ðŸ˜
Simply call FilenameSuffixer::reset() and the initial defaults will be reinstated.
Honestly, I don't know if this is going to be a useful feature - it came up in testing and it feels more like a consequence of implementing this as a static class. Probably needs some thought!