I have a script that will write a given string to a text file…
I have experience in writing to txt files via PHP….
I would like to write to a file when a form is submitted, and I have the script working to write to file ” users/user_txt/$username.txt “.
But it saves the filename as $username.txt.
This is the code:
$fp = fopen('users/user_txt/$username.txt', 'a+');
$fwrite = fwrite($fp, "," . $log_write);
This creates a blank text file and appends it, or creates file when no file exists…
However, it saves the filename as /$username.txt.
I am grabbing the variable “$username” from the form, and this echo’s correct…
But I need it to actually create the filename as:
DJ-PIMP.txt
If I submit my username as DJ-PIMP on the form…
Let’s pretend I fill the form and enter username as dave123.
I need the script to write to file like this:
$fp = fopen('users/user_txt/dave123.txt', 'a+');
How would I code this so that when I submit the form (as soon as form submitted, the variable $username would be ‘dave123’).
The code example you have above uses single quotes, which takes everything literally. Use:
Or