Write php text file

Im using php.

I want to write a php page to get parameters from another page and write to a file. AND:

  • If you already have the text of the file, it is written to a new line

  • Create one file every day

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?

+6
source share
6 answers

Thanks. I solved this problem with this http://www.redips.net/php/write-to-log-file/

-2
source

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

+6
source

If you want to create a new file every day, you can create a txt file with the current date date('dm-Y').".txt" . therefore, you can easily identify the file by date.

try under the code, I made some changes to the code and tested it.

 <?php $dateFile = date('dm-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 the file has already been created, so you can save the new entry in a new line "\n" , \ n should be in "" quatoed.

+5
source

From fopen docs :

records:

w Open for recording only; place the file pointer at the beginning of the file and trim the file to zero length. If the file does not exist, try to create it.

Append:

a Open for recording only; put the file pointer at the end of the file. If the file does not exist, try to create it.

So you want to add, not write.

If the file does not exist, it will try to create it. Therefore, just give the file a unique name that changes every day. See date examples.

 $filename = date("mdy"); // 03.10.01 
+2
source

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 a simple example to do this in php

+1
source

I haven't written a lot of php, but if you replace “w” with “w +” in fopen (), it will open the file and put the pointer at the end of it if the file already exists, which makes any fwrite () calls join the file.

 $file_handle = fopen("testFile.txt", "w+"); 

http://php.net/manual/en/function.fopen.php

edit: sachleen answered and yes, you can combine the date (up to the day) with the file name. In order for a new day to be created for this day, and if the file exists on that day, it will be added to.

0
source

All Articles