media ​
File: src/Helpers/media.php
File and media utility helpers for human-readable size formatting and safe filename generation.
Functions ​
bytesToHuman ​
php
bytesToHuman(float $bytes): stringConverts a raw byte count to a human-readable string with appropriate unit:
php
bytesToHuman(1536); // "1.5 Kb"
bytesToHuman(2097152); // "2 Mb"Units: B, Kb, Mb, Gb, Tb, Pb. Divides by 1024 at each step and rounds to 2 decimal places.
replaceAccents ​
php
replaceAccents(string $str): stringTransliterates accented and non-ASCII characters to their closest ASCII equivalent using iconv('UTF-8', 'ASCII//TRANSLIT', ...). Example: café → cafe.
sanitizeFilename ​
php
sanitizeFilename(string $filename): stringProduces a safe, lowercase filename suitable for storage and URL usage:
- Calls
replaceAccents()to remove diacritics - Replaces spaces and
%20with- - Removes all characters except alphanumerics,
-, and. - Removes all but the last
.(to keep the extension) - Collapses multiple consecutive
-into one - Removes
-immediately before. - Lowercases the result
php
sanitizeFilename('Héllo Wörld (2).JPG');
// → "hello-world-2.jpg"