8

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?

furyfish
  • 2,055
  • 5
  • 26
  • 28

6 Answers6

6

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

sgarbesi
  • 339
  • 4
  • 6
6

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.

Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
2

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
sachleen
  • 30,730
  • 8
  • 78
  • 73
2

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

Hitesh
  • 4,098
  • 11
  • 44
  • 82
0

I haven't written a lot of php, but if you replace "w" with "w+" in fopen() it'll open the file and place the pointer at the end of it if the file already exists, making any fwrite() calls append to the file.

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

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

edit: saw sachleen's answer and yes, you could combine the date (down to the day) with the file name. So that on a new day a new file would be created for that day and if the file exists for that day, it will be appended to.

mitim
  • 3,169
  • 5
  • 21
  • 25
-2

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

furyfish
  • 2,055
  • 5
  • 26
  • 28