Yup. It's great that you want to learn. But you're not going to learn anything if we do it for you.
Your algorithm is sound; you just need to translate from English to PHP. Start by learning the basic PHP syntax - how to handle variables, function calls, and control structures (such as for, which you'll be needing for this project).
Do read everything in these pages, but don't worry if you don't understand it all. Some of it deals with features or special situations that you'll rarely encounter, and is only included in the manual for the sake of completeness.
Then make ample use of php.net's function reference. If you don't know what specific function you need, find the appropriate category, and poke through the functions in that category until you find it. For example, the filesystem functions will be useful here.