I want to loop though all files in a folder and rename them. this seems possible with the rename function http://php.net/manual/en/function.rename.php
how can i keep the .jpg tag at the end and how can i loop though each item in a folder?
I want to loop though all files in a folder and rename them. this seems possible with the rename function http://php.net/manual/en/function.rename.php
how can i keep the .jpg tag at the end and how can i loop though each item in a folder?
By looping through each item in a folder and keeping the .jpg tag at the end. Which bit are you having trouble with?
// Recursive function to loop thru and do a rename
function updateDir($path,$rename,$recurse = TRUE) {
// Grab contents of dir
$dir = opendir($path);
// Loop thru each entry
while( FALSE !== ($file = readdir($dir)) ) {
// check that the current "file" is current or parent directory
if( $file == '.' || $file == '..' ) continue;
// if current "file" is a directory then...
if( is_dir($path.$file) ) {
// if recurse is set to true
if( $recurse ) {
// run function again, with current "file" as the dir parameter
updateDir($path.$file.DIRECTORY_SEPARATOR,$rename);
} else {
// otherwise continue to the next "file"
continue;
}
} else { // is a file not a directory
// get information on the file
$f = pathinfo($path.$file);
// get the original file name without extension
$name = $f['filename'];
// get the original extension
$ext = $f['extension'];
// basic rename pattern, adjust to your needs takes test.txt to
// test plus the $rename parameter plus the original extension
rename($path.$file,$name.$rename.$ext);
}
}
// close the directory
closedir($dir);
}
// example
updateDir('c:/test/',time());
Obviously you can add a lot more logic in the section controlling the renaming to give you move control over how you rename, this was just an example to get you going (hopefully)