php rename files in directory folder

[sam_ad id=”40″ codes=”true”]Hi Guys

so I had this problem where I have to rename all files in a given folder to add timestamp at the start of the file name.

i.e. a.txt becomes 1324884311_a.txt

well your new files name pattern can be totally different, so this is just an indicator.

Also this is non recursive function and works on single folder only.

You may also be interested in looking at RecursiveDirectoryIterator class (requires PHP 5.3+)

Here is how I did it

First add this function to your Util class

[geshi lang=”php” nums=”1″ target=”_self” ]


Then I called this function on my directory like this

[geshi lang=”php” nums=”1″ target=”_self” ]

You can provide extra checks that can suit your need, or ask me If you have any specific need. the underlying logic to open a directory and reading all file names under it shall remain the same.

I am sort of not so active member on stackoverflow. But using the same technique I helped someone with similar problem

If you would like to have a look at my answer there you can browse to

I hope above helps



VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Leave a Reply