Skip to content

Instantly share code, notes, and snippets.

@georgebent
Last active June 17, 2023 15:16
Show Gist options
  • Save georgebent/9da357b6afb9283595088b3a27457c46 to your computer and use it in GitHub Desktop.
Save georgebent/9da357b6afb9283595088b3a27457c46 to your computer and use it in GitHub Desktop.
PHP examples
<?php
$i = 1;
$path = 'files/'; // path to files
$separator = '-logo-'; // separator to divide the string and take the first part
$errors = [];
foreach (glob($path . '*.*') as $name) {
$filename = basename($name);
$arrayOfString = explode($separator, $filename);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$newName = $arrayOfString[0] . '.' . $ext;
$pos = strpos($filename, $separator);
if ($pos === false) {
$errors[] = $filename;
echo 'NOTICE: ', $filename, ' not renamed', PHP_EOL;
} else {
rename($name, "files/".$arrayOfString[0] . '.' . $ext);
echo $i, '. ', $filename, ' renamed to >>> ', $newName, PHP_EOL;
$i++;
}
}
if (count($errors) > 0) {
echo PHP_EOL, "ERROR. Files was not renamed: ", PHP_EOL;
$index = 1;
foreach ($errors as $value) {
echo $index, '. ', $value, PHP_EOL;
$index++;
}
}
@qWici
Copy link

qWici commented Sep 4, 2017

P O L E Z N O

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment