Skip to content

Instantly share code, notes, and snippets.

@niczak
Created December 27, 2010 22:46
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 niczak/756659 to your computer and use it in GitHub Desktop.
Save niczak/756659 to your computer and use it in GitHub Desktop.
Randomizes image file names in current directory for use w/ digital frames that don't support shuffle.
<?php
/*
randomfile.php
A quickie script that will take all images in
current directory, back them up to ./Orig and
rename all files in parent directory w/ random
numbers.
Written: 12/27/2010 - Nicholas Kreidberg
Revised: 12/27/2010 - Nicholas Kreidberg
*/
$dir = "./";
$bu = "Orig/";
$atypes = array("jpg", "gif", "png");
if(is_dir($dir))
{
if($dh = opendir($dir))
{
$i = 0;
if(!is_dir($dir.$bu))
mkdir($dir.$bu);
while(($file = readdir($dh)))
{
if(is_file($file) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $atypes)))
{
echo "Filename: $file backed-up and renamed.\n";
copy($file, $dir.$bu.$file);
$newfile = rand(1, 99999999).'.'.pathinfo($file, PATHINFO_EXTENSION);
rename($file, $newfile);
$i++;
}
}
closedir($dh);
echo "$i files processed.\n\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment