Last active
June 17, 2023 15:16
-
-
Save georgebent/9da357b6afb9283595088b3a27457c46 to your computer and use it in GitHub Desktop.
PHP examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
P O L E Z N O