473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to autoincrement a filename created with a PHP page

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
2 2861
coolsti
310 100+
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
5,058 Expert 4TB
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

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

Similar topics

0
by: Marko Maehner | last post by:
Hi, I have a strange problem with my xml file. In the schema of this xml file I have set one column to autoincrement. When I enter the data in my xml file directly, the autoincrement-column gets...
0
by: Fabio Negri Cicotti | last post by:
I have created into the SQL Server 2 tables parent/child having the parent an autoincrement primary key. E.g.: Order table ordID - tinyint ordName - string(50) Order_Detail table ordID -...
0
by: Neil | last post by:
Hi, I'm getting some strange results using the autoincrement column on my datatable. I'm populating a datatable with data from my database and displaying this in a datagrid. The first time I get...
6
by: Dennis | last post by:
I have set a DataTable and one of the columns I set "AutoIncrement" to True. I then populate the Table by setting the columns to values then add the row to the table. I inadverently set the...
1
by: Sam | last post by:
Hi all I have a simple datagrid form which has 4 columns. The first one is used just like a key and the other threes allow user to enter numbers. The problem that I am having is that when I...
6
by: Michael | last post by:
I am trying to create an access database within Net 2003 using the ADOX library which works fine except when I try to add the AutoIncrement property to the ContactId column. I am experiencing a...
5
by: JC Voon | last post by:
Hi: How to reset the autoincrement value generated by DataTable ? I've master and detail table, the detail table has a autoincrement column, each time i add a new master record, i need to...
4
by: Tim | last post by:
Hello All, I could use some help on an error that is just now popping it's head up. Seems that the autoincrement numeric has hit 32,767. The autoincrement is used in various locations in the...
1
rizwan6feb
by: rizwan6feb | last post by:
I have a DataTable with thousands of records, i want to show these records on per page basis ( i.e a DataGridView showing first 20 records and next button to show next 20 records ...) To achieve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.