Skip to content

Instantly share code, notes, and snippets.

@webinista
Last active December 19, 2016 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webinista/5f5931643d53cfbf67fb10b7aa4724e2 to your computer and use it in GitHub Desktop.
Save webinista/5f5931643d53cfbf67fb10b7aa4724e2 to your computer and use it in GitHub Desktop.
PHP: Change a file name's extension for every file in an directory.
function change_extension($filename, $index, $extension) {
if (pathinfo($filename, PATHINFO_EXTENSION) == 'md'):
$fn = sprintf("%s.%s", pathinfo($filename, PATHINFO_FILENAME), (string)$extension);
$success = rename(sprintf('./%s', $filename), $fn);
endif;
if($success) {
printf("Success! %s was renamed to %s\n", $filename, $fn);
}
}
// Sample usage
array_walk($files, 'change_extension','markdown');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment