Search by

zdenekgebauer / id-file

zdenekgebauer

handle files with ID prefix

Package info

gitlab.com/zdenekgebauer/id-file

Issues

pkg:composer/zdenekgebauer/id-file

Statistics

Installs: 4

Dependents: 0

Suggesters: 0

Stars: 0

2.1.0 2026-11-29 10:42 UTC

This package is auto-updated.

Last update: 2026-11-29 09:54:06 UTC


README

Library of functions for manipulating with files prefixed with numeric identifier (ie. 1234_file.jpg).

Files can be stored in subdirectories, based on id (ie. /images/1/2/123456_another-image.gif). Number of files in each subdirectory can be set to 10, 100, 1000 or unlimited quantity.

Requirements

PHP 8.4 or newer. No runtime dependencies.

Installation

composer require zdenekgebauer/id-file

Usage

Find files

Expected files stored in subdirectories like:

/images/1_image.jpg
/images/1/1234_another-image.gif
/images/1/2/12345_image.gif
IdFile::findFile(1, '/images', IdFile::FILES_PER_DIR_1000);
// returns /images/1_image.jpg, expects max 1000 files/directory

IdFile::findFile(1234, '/images', IdFile::FILES_PER_DIR_UNKNOWN);
// returns /images/1/1234_another-image.gif, slower, searches subdirectories for all options (10/100/1000 per directory)

If more files share the same id (e.g. 1_image.jpg and 1_image.png), the first match in alphabetical order is returned. Returns an empty string when nothing is found, including when $baseDir does not exist.

Create file name with identifier

IdFile::createFileName(1, 'file.jpg');                          // returns 1_file.jpg
IdFile::createFileName(1234, 'file with spaces.tar.gz');        // returns 1234_file-with-spaces.tar.gz
IdFile::createFileName(0, 'příliš žluťoučký kůň.txt');         // returns 0_prilis-zlutoucky-kun.txt
IdFile::createFileName(1, '../../etc/passwd');                  // returns 1_passwd, any path is stripped

Slugify a string

IdFile::slugify('Příliš žluťoučký kůň'); // returns 'Prilis-zlutoucky-kun'
IdFile::slugify('file with spaces.jpg'); // returns 'file-with-spaces.jpg'

Get file name without identifier

IdFile::stripId('1_file.jpg');                  // returns file.jpg
IdFile::stripId('1234_file-with-spaces.tar.gz'); // returns file-with-spaces.tar.gz
IdFile::stripId('1_my_file.jpg');               // returns my_file.jpg

Only the part before the first underscore is removed, without checking that it is a number. IdFile::stripId('abc_file.jpg') therefore returns file.jpg.

Create path to subdirectory

IdFile::idToPath(123456, IdFile::FILES_PER_DIR_1000); // returns '1/2/3/'
IdFile::idToPath(123456, IdFile::FILES_PER_DIR_100);  // returns '1/2/3/4/'
IdFile::idToPath(123456, IdFile::FILES_PER_DIR_10);   // returns '1/2/3/4/5/'
IdFile::idToPath(123456, IdFile::FILES_PER_DIR_UNLIMITED); // returns ''
IdFile::idToPath(5, IdFile::FILES_PER_DIR_1000);      // returns '' (ID shorter than depth)

Delete file

Expected files stored in subdirectories like:

/images/1/2/3/123456_file.jpg
/images/1/2/3/123456_file.gif
/images/1/2/3/123456_file.png
IdFile::delete(123456, '/images', '', IdFile::FILES_PER_DIR_1000);              // delete all matching files
IdFile::delete(123456, '/images', '123456_file.gif', IdFile::FILES_PER_DIR_1000); // delete files except 123456_file.gif
IdFile::delete(123456, '/images', '', IdFile::FILES_PER_DIR_UNKNOWN);            // delete in all subdirectories, slower

Empty subdirectories left behind are not removed. Deleting from a non-existent directory does nothing.

Exceptions

All methods throw \InvalidArgumentException:

ConditionMethods
negative $fileIdcreateFileName(), delete(), findFile(), idToPath()
empty $fileNamecreateFileName()
$filesPerDir outside FILES_PER_DIR_10/_100/_1000/_UNLIMITEDidToPath()

Note that idToPath() rejects FILES_PER_DIR_UNKNOWN as well — that value is only meaningful for findFile() and delete(), which search every possible layout. These two do not throw for any other unknown $filesPerDir, they treat it as FILES_PER_DIR_UNLIMITED.

delete() and findFile() accept a $baseDir containing glob wildcards (*, ?, [, ]); they are matched literally. A backslash in a directory name is not escaped, because it is a directory separator on Windows.

Development

Local environment runs on Lando (Docker, PHP 8.5 + xdebug):

lando start          # start the environment
lando install        # install dependencies
lando tests          # run the test suite
lando coverage       # test suite with code coverage
lando ecs            # coding standards (lando ecs-fix to apply)
lando infection      # mutation testing, requires 100 % MSI
lando phpstan        # static analysis, level max
lando rector         # modernization dry-run (lando rector-fix to apply)

License

MIT — see LICENSE.