0

我在服务器上有很多带有图片的文件夹,文件名是随机的而且很多,但每个文件都是 jpg。我需要将任何给定文件夹上的每个文件重命名为时间戳信息。

例子。文件名 1.jpg , 2.jpg

大会后 12/15/11 10:20:58.jpg , 12/15/11 11:21:50.jpg 。

实际的日期格式并不重要。

PS重要的是它只修改jpg文件而不是文件夹名称或子文件夹内容。

谢谢。

4

2 回答 2

0

您可以使用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');

}

这是来自记忆,因此可能存在一些问题,但我相当确定这就是您想要的。

于 2012-11-08T05:25:16.317 回答
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
于 2012-11-08T05:55:24.657 回答