Page 1 of 1

appending a file based on file name in php

Posted: Thu Feb 03, 2011 10:10 am
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?

Re: appending a file based on file name in php

Posted: Thu Feb 03, 2011 10:24 am
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;
}

Re: appending a file based on file name in php

Posted: Thu Feb 03, 2011 10:26 am
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