Contact form with unique file name - php

I want to create php file for my contact form but whenever they submit I want a different file name.txt.
This is the code. Whenever someone else submits I lose the old one.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST ['message'];
$file = fopen('contactform.txt', "w+");
$content = $name. PHP_EOL .$email. PHP_EOL .$message;
header('Location: http://localhost:8080/site-edits/contact.php');
fwrite($file , $content);
fclose($file);
?>

Like Barry said, you could append a timestamp to your name.
PHP also has a function tempnam available that generates a unique filename:
$file = fopen(tempnam(".", "contactform.txt"), "w+");

One thing you can do is add the current date to the file name:
$file = fopen('contactform-'.date('YmdHis').'.txt', "w");
Alternatively, you can append to the file instead of writing over it.
$file = fopen('contactform.txt', "a");
$content = $name. PHP_EOL .$email. PHP_EOL .$message .PHP_EOL.PHP_EOL;
Most people use a database (for example MySQL) for this. I'd suggest learning about how to use databases when you get the time.

You can either create new file with different name any time user submits the form or append text in existed file usin 'a' rather 'w+'
Like:
$file = fopen('contactform.txt', 'a');
For more go to php file handling

Related

CSV from a form php

I'm a begginer in PHP and I want to get data from a form which contains a name,a comment and a name of a photo the user selects, and put it in a csv.But when i try to write the date, the data i already have in the csv is overwrited.So every time I only have one line in my csv with the newest data.
I want data to be introduced in a new csv line everytime the form is used like this:
Russel,Hello,Football.jpg
James,Bye,Coke.png
Instead of being overwrited like this:
James,Bye,Coke,png
This is what i tried:
if (isset($_POST['submit'])) {
$name = $_POST["nom"];
$comment = $_POST['com'];
$image = $_FILES['imag']['name'];
$csvfile = 'fichero.csv';
$fvisitas = fopen($csvfile, "c+");
$lista = array(
array($name, $comment, $image)
);
foreach ($lista as $campos) {
fputcsv($fvisitas, $campos);
}
fclose($fvisitas);
}
You should open your file with the append flag
$fvisitas = fopen($csvfile, "a");
This will let you append lines instead.
You should use a+ mode.
For more about fopen modes refer to fopen
$fvisitas = fopen($csvfile, "a+");

Printing or Echo form data to csv file

I have listened to a youtube tutorial severally in order to write a php code for online form data and print to CSV file on my website. The code the teacher used on the tutorial works perfectly on my local computer but doesn’t print anything on the CSV file as I have now uploaded the page on my website.
when site users fill the form online and click on submit button on my site – no information from form is printed on the same CSV file. Why?
This is the code I got from the tutorial and wrote the same:
<?php
if(isset($_POST['submit'])){
$names = $_POST['names'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$job = $_POST['job'];
$city = $_POST['city'];
$data = $names . "," . $telephone . "," . $email . "," . $job . "," . $city;
$file = "cardealer.csv";
file_put_contents ($file, $data . PHP_EOL, FILE_APPEND);
echo "Thank you for completing this form, we will reply soon";
}
?>
How can I fix this problem sir, I used the exact offline code you gave in the tutorial.
I appreciate your kind gestures here to help starters like us.
Thank you,
this to export excel file based on CSV data and download the file
if you want to write a file you should consider using chmod
like this
chmod($folder_path. $file_name. ".".$ext, '775');
also in this part
fopen("php://output",w);
you can change it to
fopen($path_to_file,w);
you can change the MIME-TYPE and the file extension to what ever you want.
/**
*the $array the data to be converted to CSV
*
*/
function array_to_csv_download($array, $filename = "export.xls", $delimiter=";") {
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename="'.$filename.'";');
header("Pragma: no-cache");
header("Expires: 0");
// open the "output" stream
$f = fopen('php://output', 'w');
foreach ($array as $line) {
fputcsv($f, $line, $delimiter);
}
}

PHP How to generate new file txt per day with data

I've created data collection for a specific txt file. I need the script to separate records per days.
For example: today is 100 data and i need have a log16Jan.txt file, tomorrow we have 52 data and i need have a log17Jan.txt file?
How i can do that?
<?php
$name = $_POST['name'];
$tekst = "Name: $namee \r\n";
$uchwyt = fopen("log.txt", "a");
fwrite($uchwyt, $tekst);
fclose($uchwyt);
header("Location: http://#/complete.html");
?>
You can get the date using the date() function and add it to the file name, like this:
$name = $_POST['name'];
$tekst = "Name: $name \r\n";
// Adding today's date to the log file
$uchwyt = fopen("log".date("dM").".txt", "a");
fwrite($uchwyt, $tekst);
fclose($uchwyt);
header("Location: http://#/complete.html");
You could use the date() function.
And since you are creating log files you should consider using a log file name that will sort natively in date order in a directory listing. This will make it easier to find a specific log file in a large listing of files. It will also give you more log file cleanup/removal options.
For example:
// 2018-01-16-log.txt
$logFilename = date("Y-m-d") . "-log.txt";
$uchwyt = fopen($logFilename, "a");
UPDATE: per your request about adding a line number before each log line, there are many ways to do this and some depend on your server environment (Linux/Windows). You could read each line from the log and keep a counter. Or you could use a server utility, like Linux wc (Word Count). Here's one way to do it:
<?php
// 2018-01-16-log.txt
$logFilename = date("Y-m-d") . ".log";
echo "Log file = $logFilename\n";
if (! file_exists($logFilename)) {
$count = 0;
} else {
// use Linux wc utility
$count = system("wc -l < $logFilename");
$count = trim($count);
}
echo "Lines = '$count'\n";
$name = $_POST['name'];
if (! $name) {
$name = "NONE";
}
$tekst = "$count. Name: $name \n";
$uchwyt = fopen($logFilename, "a");
fwrite($uchwyt, $tekst);
fclose($uchwyt);
?>

Writing a new and appending a file in PHP without erasing contents

How could one write a new line to a file in php without erasing all the other contents of the file?
<?php
if(isset($_POST['songName'])){
$newLine = "\n";
$songName = $_POST['songName'];
$filename = fopen('song_name.txt', "wb");
fwrite($filename, $songName.$newLine);
fclose($filename);
};
?>
This is what the file looks like
Current view
This is what is should look like Ideal View
Simply:
file_put_contents($filename,$songName.$newLine,FILE_APPEND);
Takes care of opening, writing to, and closing the file. It will even create the file if needed! (see docs)
If your new lines aren't working, the issue is with your $newLine variable, not the file append operations. One of the following will work:
$newLine = PHP_EOL; << or >> $newLine = "\r\n";
You have it set for writing with the option w which erases the data.
You need to "append" the data like this:
$filename = fopen('song_name.txt', "a");
For a complete explanation of what all options do, read here.
To add a new line to a file and append it, do the following
$songName = $_POST['songName'];
$filename = fopen('song_name.txt', "a+");
fwrite($filename, $songName.PHP_EOL);
fclose($filename);
PHP_EOL will add a new line to the file

Appending data to a csv file from a php form but also adding static data every time

Ok I almost have it working but am having trouble escaping the html to preserve the link I'm missing something here .. I'm sure it's simple
if($_POST['formSubmit'] == "Submit")
$varUserName = $_POST['username'];
$varPW = $_POST['PW'];
$varEmail = $_POST['email'];
{
$fs = fopen("testcsv.csv","a");
fputcsv($fs, array($varUserName,$varPW,$varEmail,"admin","title",",category","some text here site.com",));
fclose($fs);
exit;
}
?>
I am not sure what you're having trouble with. Are you familiar with fopen();? Are you familiar with CSV?
You will need to open the file for appending and append to your file using fwrite(). Once done, close the file with fclose().
$fp = fopen("./filename", 'a'); //Open file for append
//fwrite($fp, $row1.",".$row2); //Append row,row to file
fputcsv($fp, array($name,$password,"http://$name.whatever.com")); //#Optimist
fclose($fp); //Close the file to free memory.

Categories