Sm00rf9000 Sm00rf9000 - 1 year ago 67
HTML Question

php create new .txt file with specific name ending

I have a site which has an HTML form that forwards some information to php code. Then once the php document has gotten the code it creates a new document

and then it writes two values to it. Only problem is that when this has been done one time, it will just append to that same document as it has already been created.

I was then wondering if there is a way to change the naming of the new document, so lets say it would look at the current documents in the folder and then lets say there are no documents, then it would be
or if there were three documents, then the new txt file would be called
. To make sure it doesn't just write the new info to a current document.

Any help is very much appreciated.

Here is my current php file


$field_fname = $_POST['fnameInput'];
$field_email = $_POST['emailInput'];

$filename = "../public_html/NO/newDocu.txt";

$f_data= '
First Name : '.$field_fname.'
Email : '.$field_email.'

echo 'Thank you!';

$file = fopen($filename, "a");

echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.html">';


Answer Source

Add this code just after your $filename variable:

$mainname= $filename;
$version = 1;
    $filename = str_replace('.txt', $version . '.txt', $mainname);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download