Optiq
Optiq

Reputation: 3192

How do I go about incrementing a file name created from input data using php?

I created a script for saving a form submission as a .txt file that uses the user's first name as the file name. To keep it simple I created a test file in HTML to show what I'm trying to achieve.

<form action="sender.php" method="post">

    <input type="text" id="f_name" name="First_Name" />
    <input type="text" id="demo_1" name="Demo_01" />
    <input type="text" id="demo_2" name="Demo_02" />
    <input type="text" id="demo_3" name="Demo_03" />
    <input type="text" id="demo_4" name="Demo_04" />
    <input type="text" id="demo_5" name="Demo_05" />
    <input type="text" id="demo_6" name="Demo_06" />



    <input type="submit" value="submit" />

</form>

So far the script I have for processing the form in PHP is this

<?php ini_set('display_errors','on'); ?><?php


$data= "";

foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}


$fileName= fopen("Submissions/".$_POST['First_Name'],'w');

fwrite($fileName, $data);
fclose($fileName);

?>

What I want to do is add some code to make it increment the file name so in the event of me getting multiple submissions from people with the same first name it won't over write them. Do say I have a submission from somebody named Bob, and two more Bobs happens to fill out the questionnaire, I want it to save as

Bob.txt Bob_02.txt Bob_03.txt

This means I need something that will be able to ignore the "_02" at the end to identify the "Bob" at the beginning so it doesn't go

Bob.txt Bob_02.txt Bob_02_02.txt

I came up with this in an attempt to do just that but got errors on both of the "file_exists" opperators

if (file_exists($fileName)){
$num="00";
$fileNameUpd= count(substr.$_POST['First_Name'].$num++);

fopen("Submissions/".$fileNameUpd,'w');
fwrite($fileNameUpd, $data);
fclose($fileNameUpd);
}

else if (!file_exists($fileName)){

fwrite($fileName, $data);
fclose($fileName);
};

the way I added it in is like this

<?php ini_set('display_errors','on'); ?><?php


$data= "";

foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}


$fileName= fopen("Submissions/".$_POST['First_Name'],'w');

if (file_exists($fileName)){
$num="00";
$fileNameUpd= count(substr.$_POST['First_Name'].$num++);

fopen("Submissions/".$fileNameUpd,'w');
fwrite($fileNameUpd, $data);
fclose($fileNameUpd);
}

else if (!file_exists($fileName)){

fwrite($fileName, $data);
fclose($fileName);
};

?>

How can I get this to achieve what I need it to do?

Upvotes: 2

Views: 321

Answers (2)

Tintu C Raju
Tintu C Raju

Reputation: 2700

Check the file name and loop till dynamic file name not exists. Each time append a counter variable with the file name.

<?php 
    $name = "Submissions/".$_POST['First_Name'].".txt";
    $actual_name = pathinfo($name,PATHINFO_FILENAME);
    $original_name = $actual_name;
    $extension = pathinfo($name, PATHINFO_EXTENSION);
    $i = 1;
    while(file_exists("Submissions/".$actual_name.".".$extension))
    {           
        $actual_name = (string)$original_name."_".$i;
        $name = $actual_name.".".$extension;
        $i++;
    }
    file_put_contents("Submissions/".$name, $data);
?>

Upvotes: 1

Tristan
Tristan

Reputation: 3321

You can run while loop to check if the file already exists. As others have pointed out, first name is not a good unique identifier so perhaps you could consider the naming convention.

<?php 
ini_set('display_errors','on');

$data= "";
foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}

$file = $_POST['First_Name'].'.txt';
$i = 0;
while (is_file("Submissions/".$file)) {
    $file = $_POST['First_Name'].'_'.$i.'.txt';
    $i++;
}

$fileName= fopen("Submissions/".$file,'w');
fopen("Submissions/".$fileName,'w');
fwrite($fileName, $data);
fclose($fileName);

?>

Upvotes: 0

Related Questions