Jump to content

incremented variable within a variable


tadakan

Recommended Posts

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

Link to comment
Share on other sites

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));
}

 

Link to comment
Share on other sites

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.)

 

Link to comment
Share on other sites

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]);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.