furyfish
furyfish

Reputation: 2135

Write text file php

Im using php.

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

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?

Upvotes: 8

Views: 38747

Answers (6)

Hitesh
Hitesh

Reputation: 4298

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

Upvotes: 2

furyfish
furyfish

Reputation: 2135

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

Upvotes: -2

Devang Rathod
Devang Rathod

Reputation: 6736

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.

Upvotes: 6

sgarbesi
sgarbesi

Reputation: 339

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

Upvotes: 6

sachleen
sachleen

Reputation: 31141

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

Upvotes: 2

mitim
mitim

Reputation: 3201

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.

Upvotes: 0

Related Questions