Last active
July 20, 2022 08:57
-
-
Save Jany-M/e25e12967a05f88abf0f0eae34936f03 to your computer and use it in GitHub Desktop.
[PHP] Rename files in directory subdirectories
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 | |
/*------------------------------------------------------------------------------------------------ | |
This specific case assumes the following structure. Please note the absence of file extensions. | |
MAIN_DIR | |
- xyz (dir) | |
-- abc123-jpg (file) | |
-- xyz456-png (file) | |
-- jkl123-jpg-300x300 (file) | |
-- oiu456-png-300x300 (file) | |
- def (dir) | |
-- vbn456-png (file) | |
-- lkj123-jpg-300x300 (file) | |
- etc. (dirs) | |
---------------------------------------------------------------------------------------------- */ | |
// This part is only useful if you are using WordPress ... | |
// ... and if the files you want to rename are in a subdirectory of /wp-content/uploads | |
/*$wp_upload = wp_upload_dir(); | |
$wp_upload_dir = trailingslashit( $wp_upload["basedir"] ); | |
$folder_name = trailingslashit( 'test_rename' ); // change this | |
$uploads_dir = trailingslashit( $wp_upload_dir . $folder_name);*/ | |
$uploads_dir = '/path/to/dir/'; // change this if you are not using the part above, otherwise delete line | |
// Retrieves subdirectories | |
$directories = glob($uploads_dir . '*' , GLOB_ONLYDIR); | |
// Let's set what we need to change and what we need to replace it with | |
$change_1 = '-jpg'; | |
$replace_1 = '.jpg'; | |
$change_2 = '-png'; | |
$replace_2 = '.png'; | |
$remove_1 = '-jpg-'; | |
$remove_2 = '-png-'; | |
foreach($directories as $dir) { | |
if ($handle = opendir($dir)) { | |
while (false !== ($fileName = readdir($handle))) { | |
// skip weird stuff | |
if($fileName == '.' || $fileName == '..') | |
continue; | |
// skip if file already has extension in place | |
$ext = substr($fileName, -4); | |
if($ext == $replace_1 || $ext == $replace_2) | |
continue; | |
// cleanup | |
if(preg_match('/'.$remove_1.'/',$fileName)) { | |
$newName = str_replace($remove_1,'-',$fileName); | |
rename($dir.'/'.$fileName, $dir.'/'.$newName.$replace_1); | |
} elseif(preg_match('/'.$remove_2.'/',$fileName)) { | |
$newName = str_replace($remove_2,'-',$fileName); | |
rename($dir.'/'.$fileName, $dir.'/'.$newName.$replace_2); | |
} | |
// first replacement | |
elseif (preg_match('/'.$change_1.'/',$fileName)) { | |
$newName = str_replace($change_1,$replace_1,$fileName); | |
rename($dir.'/'.$fileName, $dir.'/'.$newName); | |
} | |
// second replacement | |
elseif(preg_match('/'.$change_2.'/',$fileName)) { | |
$newName = str_replace($change_2,$replace_2,$fileName); | |
rename($dir.'/'.$fileName, $dir.'/'.$newName); | |
} | |
} | |
closedir($handle); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment