rm/unique

Library for generating unique file names.

v0.1.0 2015-04-26 10:07 UTC

This package is auto-updated.

Last update: 2024-03-05 19:06:54 UTC


README

Build Status Code Quality Code Coverage Packagist

Uniqe is simple library for generating unique filenames in directories.

Requirements

Unique requires PHP 5.4 or later.

Installation

The best way to install Unique is use Composer package rm/unique or manual download the latest ZIP package from GitHub.

$ composer require rm/unique

Example

I need upload file something.png into directory /images.

If it is necessary to not overwrite existing files, you need to generate unique filenames.

Now it's simple!

$filename = Unique::get('something.png', '/images'); // return 'something.png'

And what if directory contains files something.png and for example, also something-1.png?

Returns something-2.png!

Unique::get() automatic generate filename in format <filename><separator><order>.<extension> and check if exists in specified directory. If is unique, return it.

API

get()

Method get() has two parameters:

  • string $filename Name of file
  • string $dir Directory where will be file saved

Return

  • string Output filename

$separator

Default separator of filename and order it's -. It's possible change it, for example:

Unique::$separator = '|';

Contributing

  • Use it!
  • Write bug reports of ideas into Issue tracker.
  • Fork repos and send pull requests with number of issue, source code and tests.

Contact

Roman Mátyus romanmatyus@romiix.org