Jump to content

Writing to a text file instead of database [RESOLVED]


Recommended Posts

I have a variable that I need to change on a regular basis and it needs to be done using a form.
I don't want to create a table in a database just for this one variable so would like to know how i can write it to a text document

Here is the code in index.php that creates an thermometer image.  The bit that needs to be changed is the [u]current[/u]. 
[code]<img src="/includes/thermometer/thermo.php?current=10000&max=15000" hspace="55">[/code]

I thought saving the current variable to a text file would work unless there is a way of updating the variable within index.php

Has anyone got any ideas how I could do this?
something like this:

[code]<?php
$name = $_POST['name']; // vars from form
$filename = 'data.txt'; // textfile name

if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $name) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
header ('Location: thanks.php');
?> [/code]
It's very easy, look at these functions:

For reading: [url=http://uk.php.net/manual/en/function.file-get-contents.php]file_get_contents()[/url]

For Writing: [url=http://uk.php.net/manual/en/function.fopen.php]fopen()[/url], [url=http://uk.php.net/manual/en/function.fwrite.php]fwrite()[/url], [url=http://uk.php.net/manual/en/function.fclose.php]fclose()[/url]

Regards
Huggie
To delete the contents of a text file try:

[code]
<?php

if ( isset ( $_POST['submit'] ) ) {

$file = "whatever.txt";

$fp = fopen( $file, "w" );

if ( fwrite ( $fp, "" ) ) {

    print "The file $file has been erased!";

} else {

    print "Writing to the file: $file has failed!";

}

}

?>
[/code]

If you would like to overwrite the file, put text in the empty quotes in this line:

[code]
if ( fwrite ( $fp, "This will overwrite the text!" ) ) {
[/code]

Then replace that line with the other line of that code.


As for updating text try doing this:

[code]
<?php

if ( isset ( $_POST['submit'] ) ) {

$file = "whatever.txt";

$fp = fopen( $file, "a" ); // added append which is what "a" means

if ( fwrite ( $fp, "text1<br>text2<br>text3<br>text4<br>text5" ) ) {

    print "The file $file has been appended too!";

} else {

    print "Writing to the file: $file has failed!";

}

}

?>
[/code]

-Dan M.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.