By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,349 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,349 IT Pros & Developers. It's quick & easy.

How to autoincrement a filename created with a PHP page

P: 40
I have a process in a PHP page that creates a file, and it works fine:
Expand|Select|Wrap|Line Numbers
  1. if ("$_REQUEST[severity]" == "Level1-No_Obvious_Harm") {
  2.      $sevvar = "1";
  3. } elseif ("$_REQUEST[severity]" == "Level2-Non-permanent_Harm") {
  4.      $sevvar = "2";
  5. } elseif ("$_REQUEST[severity]" == "Level3-Semi-permanent_Harm") {
  6.      $sevvar = "3";
  7. } elseif ("$_REQUEST[severity]" == "Level4-Major_Permanent_Harm") {
  8.      $sevvar = "4";
  9. } elseif ("$_REQUEST[severity]" == "Level5-Death") {
  10.      $sevvar = "5";
  11. } else {
  12.      $sevvar = "Nada";
  13. }
  14.  
  15.   $file = "notify/$sevvar".".$_REQUEST[fac_id]".".txt";
  16.   $handle = fopen($file, 'w');
  17.   fclose($handle);
  18.  
The name is based on a severity level data value and hospital name.

I now need to increment the filename since it is possible to need more than one file with the same severity level and hospital.

I see where autoincrementing a database field is covered in PHP, but what about autoincrementing a filename that is created by PHP?

TIA,

jej1216
Sep 24 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 310
I don't think you can autoincrement the filename using just PHP.

One way to do it would be to do a directory search of all filenames that match your basic name without the incremental number, extract the incremental number part and find out the largest value. Then create a filename by incrementing the largest value by 1.

What I do not know about here is how to avoid concurrency issues with multiple users. If for example two persons run the same script at the same time, both scripts may locate the same "largest value" and attempt to create the same "next value" filename. You would need some kind of lock on this or at least a bail-out-and-try-again scheme when coming to the line that actually performs the file creation (by first looking if the target file still does not exist).

Maybe someone else can answer the concurrency issue here.
Sep 25 '08 #2

Atli
Expert 5K+
P: 5,058
Hi.

Personally I would just add the current timestamp plus a string of a random number, like say 100-999.
That would make a pretty unique number that you could add to you filename.

Or you could try adding the total number of existing files to your new filename. This may be a little slower than the other method, seeing as it has to do extra file-system operations, but may look slightly better.

Both have a slight chance of causing a collision, but that would be rare and easily manageable.
Sep 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.