Created
November 12, 2014 12:26
-
-
Save renekorss/2c25e69f6b430c97dac5 to your computer and use it in GitHub Desktop.
Rename files in subfolders
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 | |
function glob_recursive($pattern, $flags = 0) { | |
$files = glob($pattern, $flags); | |
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { | |
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); | |
} | |
return $files; | |
} | |
$root = $_GET['root']; | |
$deletedCount = 0; | |
$renamedCount = 0; | |
// Get all files in root folder | |
$files = glob_recursive($root.'/*'); | |
for($i=0;$i<count($files);$i++) { | |
if(stripos($files[$i], "Thumbs.db")) { | |
unlink($files[$i]); | |
$deletedCount++; | |
} | |
else { | |
// Get file extension | |
$ext = substr($files[$i], strlen($files[$i])-3,3); | |
// Rename all .JPG files to .jpg | |
if (preg_match("/(JPG)/",$ext)) { | |
$filename = substr($files[$i], 0, strlen($files[$i])-3); | |
$renameFile = $filename.$ext; | |
$newfile = $filename.'jpg'; | |
if(rename($files[$i], $newfile)) | |
echo $filename.$ext.' renamed to '.$newfile.'<br />'; | |
else | |
echo 'Renaming file '.$renameFile.' failed.'; | |
$renamedCount++; | |
} | |
} | |
} | |
echo "<br /><br /><strong>$renamedCount image files renamed.</strong> $deletedCount Thumbs.db files deleted."; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment