PHP code example of ondrej-vrto / php-filename-sanitize
1. Go to this page and download the library: Download ondrej-vrto/php-filename-sanitize library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ondrej-vrto / php-filename-sanitize example snippets
use OndrejVrto\FilenameSanitize\FilenameSanitize;
$filename = '[file#name].jpg';
$sanitize_filename = FilenameSanitize::of($filename)->get();
// Output: file-name.jpg
// classic object
(new FilenameSanitize($filename))->get();
// or static instance
FilenameSanitize::of($filename)->get();
FilenameSanitize::of('/some#/di[]r/file#name.jpg')
->withSubdirectory()
->get();
// Output: \some\dir\file-name.jpg
FilenameSanitize::of('/some#/di[]r/file#name.jpg')
->addSubdirectoryToFilename()
->get();
// Output: some-dir-file-name.jpg
FilenameSanitize::of('/some#/di[]r/file#name.jpg')
->withBaseDirectory("/base/directory")
->get();
// Output: \base\directory\file-name.jpg
// together
FilenameSanitize::of('/some#/di[]r/file#name.jpg')
->withBaseDirectory("/base/directory")
->addSubdirectoryToFilename()
->withSubdirectory()
->get();
// Output: \base\directory\some\dir\some-dir-file-name.jpg
FilenameSanitize::of('file_name.jpg')
->withNewExtension('webp')
->get();
// Output: file-name.webp
FilenameSanitize::of('file_name.jpg')
->addActualExtensionToFilename()
->get();
// Output: file-name-jpg.jpg
// together
FilenameSanitize::of('file_name.jpg')
->addActualExtensionToFilename()
->withNewExtension('webp')
->get();
// Output: file-name-jpg.webp
FilenameSanitize::of('file_name.jpg')
->widthFilenameSuffix('suffix')
->widthFilenamePrefix('prefix')
->get();
// Output: prefix-file-name-suffix.jpg
FilenameSanitize::of(null)
->get();
// throw ValueError exception
FilenameSanitize::of(null)
->defaultFilename('default-file-name.jpg')
->get();
// Output: default-file-name.jpg
FilenameSanitize::of('File*NAME.Ext')
->disableLowerCase()
->get();
// Output: File-NAME.Ext
FilenameSanitize::of('file#name.jpg')
->customSeparator('_-_')
->get();
// Output: file_-_name.jpg
FilenameSanitize::of('file#name.ext')
->customSeparator('ef')
->get();
// Output: ileefnam.xt
FilenameSanitize::of('foo2\bar2\file-name.jpg')
->addActualExtensionToFilename()
->withBaseDirectory('C:/foo/bar')
->widthFilenameSuffix('surfix')
->widthFilenamePrefix('prefix')
->addSubdirectoryToFilename()
->withNewExtension('webp')
->customSeparator('_')
->withSubdirectory()
->get();
// Output: C:\foo\bar\foo2\bar2\prefix_foo2_bar2_file_name_surfix_jpg.webp