tadakan Posted June 19, 2011 Share Posted June 19, 2011 I'm trying to figure out how to do this, or if there's a better way to do it, I'd be happy to hear it. Basically I'm creating a text (.bat) file as part of a PHP CLI script that is triggered by email piping on receipt of an email. The PHP script will parse the email for certain bits and then either create and activate the .bat file or just ignore it. (probably will log in to an email box and change the imap label of the email as another level of logging, but that's a future tribulation.) The worry that I had that I'm trying to guard against is multiple emails coming through near-simultaneously and overwriting the .bat file before it fires so I wanted to create a a function that will check for the existence of the file and if it exists, increment the file name. The problem I seem to be running in to is that I've already set the file path as the variable $filename and so it doesn't get incremented inside of $filename when I increment $i. The following is just a snippet that I was using to test the issue. $i = 1; $filename = ("C:\\temp\\output".$i.".txt"); do{ $i++; echo $filename;} while ($i<=5); Thanks Tom Quote Link to comment https://forums.phpfreaks.com/topic/239772-incremented-variable-within-a-variable/ Share on other sites More sharing options...
2-d Posted June 19, 2011 Share Posted June 19, 2011 You could always do a check to see if the file exists already and then increment it like so: <?php $i = 1; $filename = ("C:\\temp\\output".$i.".txt"); if(file_exists($filename)){ do{ $i++; $filename = ("C:\\temp\\output".$i.".txt"); } while(!file_exists($filename)); } Quote Link to comment https://forums.phpfreaks.com/topic/239772-incremented-variable-within-a-variable/#findComment-1231679 Share on other sites More sharing options...
tadakan Posted June 19, 2011 Author Share Posted June 19, 2011 are you sure while(!file_exists($filename)); is valid? I can't find any mention of using an exclamation point with a built-in function like that and it doesn't seem to be working for me (but I might just not be using it correctly.) Quote Link to comment https://forums.phpfreaks.com/topic/239772-incremented-variable-within-a-variable/#findComment-1231681 Share on other sites More sharing options...
tadakan Posted June 19, 2011 Author Share Posted June 19, 2011 I got it to do what (I think) I needed it to do: //Set the path and filename $filename = ("C:\\temp\\output".$i.".txt"); //if the file exists renumber the filename and rewrite the variable while(file_exists($filename)){ $i++; $filename = ("C:\\temp\\output".$i.".txt"); } //create the next highest filename and write to the file. $handle = fopen($filename, "a"); fwrite($handle, $comp[1]); Quote Link to comment https://forums.phpfreaks.com/topic/239772-incremented-variable-within-a-variable/#findComment-1231684 Share on other sites More sharing options...
tadakan Posted June 19, 2011 Author Share Posted June 19, 2011 I'm still having issues, but they aren't related to the problem with incrementing anymore so I guess I'll start a new thread with a new topic. Quote Link to comment https://forums.phpfreaks.com/topic/239772-incremented-variable-within-a-variable/#findComment-1231693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.