appending a file based on file name in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

appending a file based on file name in php

Post by Obadiah »

say i have a series of files in a folder (area1.php,area2.php,area3.php....area75.php). What I want to do is create a script that finds the last one (area75.php if sorted by name) and echos the new name(area76.php). How would i go about creating this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: appending a file based on file name in php

Post by John Cartwright »

Perhaps something like

Code: Select all

function getNextArea($dir) 
{
   $files = array();
   foreach (glob("area*.php") as $filename) {
      $files[] = basename(substr($filename, -4));
   }
   return max($files) + 1;
}
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: appending a file based on file name in php

Post by Jade »

I'm not sure why you would create a new php file that many times...that's what databases are for. In any case you should look at these two manual pages:

http://www.php.net/manual/en/function.natsort.php
http://us3.php.net/manual/en/function.readdir.php
Post Reply