Im using php.
I want to write a php page to get parameters from another page and write to a file text. And:
If already have file text, it write to a new line
Each day create one file text
Example:
register.php
<body>
<form action='process.php' method='GET'>
Name: <input type='text' name='name'/>
<br/>
Age: <input type='text' name='age'/>
<br/>
<input type='submit' value='SUBMIT'/>
</form>
</body>
process.php
$name = $_GET['name'];
$age = $_GET['age'];
$file_handle = fopen("testFile.txt", "w");
$file_contents = "name:" . $name . "age:" . $age;
fwrite($file_handle, $file_contents);
fclose($file_handle);
print "file created and written to";
How can I do this?
PHP Write to File - fwrite() The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.
Definition and Usage The fwrite() writes to an open file. The function will stop at the end of the file (EOF) or when it reaches the specified length, whichever comes first.
The file_put_contents() function in PHP is an inbuilt function which is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.
Use the file_get_contents() and file_put_contents() functions.
Example:
$file = 'output.txt';
$buffer = 'my new line here';
if (file_exists($file)) {
$buffer = file_get_contents($file) . "\n" . $buffer;
}
$success = file_put_contents($file, $buffer);
http://php.net/manual/en/function.file-put-contents.php
http://php.net/manual/en/function.file-get-contents.php
If you want to create new file for everyday , you can create txt file with current date date('d-m-Y').".txt"
. so you can easily identify file by date.
try below code, i have made some change in code and test it.
<?php
$dateFile = date('d-m-Y').".txt";
$dataString = "name:" . $name . "age:" . $age."\n";
$fWrite = fopen($dateFile,"a");
$wrote = fwrite($fWrite, $dataString);
fclose($fWrite);
print "file created and written to";
?>
If file already created so you can store new record in new line by "\n"
, \n must be in "" quatoed.
From the fopen docs:
write:
w Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
append:
a Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
So you want to append, not write.
If the file doesn't exist, it'll try to create it. So just give the file a unique name that changes with the day. See date docs for examples.
$filename = date("m.d.y"); // 03.10.01
file_write_php
<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "Jane Doe\n".PHP_EOL;;
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
Above is simple example to do it in php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With