0

I have a lot of folders with pictures on server, the file names are random and a lot, but every file is a jpg. I need to rename every file on any given folder to the information on the timestamp.

Example. File names 1.jpg , 2.jpg

After convention 12/15/11 10:20:58.jpg , 12/15/11 11:21:50.jpg .

The actual Date format is not important.

P.S. Important that it only modifies the jpg files and not the folder names nor sub-folder content.

Thanks.

  • your problem is solve or still searching for some logic..... – Ankur Saxena Nov 08 '12 at 05:51
  • Yes i'm still searching. Your answer works but i need it on a bulk scale, the folders contain more than 1000 files each. I am curretly trying out Robbo's answer, and am getting an error. I will kip trying, thanks. – Carlos A. Padron C. Nov 08 '12 at 13:19

2 Answers2

0

You can use DirectoryIterator.

$dir = new DirectoryIterator('path/to/files');
foreach ($dir AS $file)
{
    if ($file->isDot() OR $file->isDir() OR $file->getExtension() != 'jpg')
    {
        continue;
    }

    // This is just doing it to a timestamp, wrap filemtime in data() to format the name how you like
    rename($file->getPathname(), $file->getPath() . DIRECTORY_SEPARATOR . filmtime($file->getPathname()) . '.jpg');

}

This is from memory so there might be some issues but I am fairly sure that is what you wanted.

Robbo
  • 2,116
  • 1
  • 19
  • 21
0
$req_file="newname.png";//required file
echo $file_name=strtotime(date("m-d-Y H:i:s"));//convert time to string 
$new_filename=$file_name.".png";//creating new file name

$path="C:\\wamp\\www\\practice\\";//path provides
rename("C:\\wamp\\www\\practice\\newname.png", "C:\\wamp\\www\\practice\\".$new_filename);//rename of file
#rename($path.$req_file, $path.$new_filename);//another way of rename of file

convert file name into string stamp like 1344664010.png
Ankur Saxena
  • 629
  • 3
  • 13
  • 26