Jump to content


Photo

Writing to a text file instead of database [RESOLVED]


  • Please log in to reply
5 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 11 October 2006 - 11:30 AM

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 current
<img src="/includes/thermometer/thermo.php?current=10000&max=15000" hspace="55">

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?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 11 October 2006 - 11:36 AM

something like this:

<?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');
?>

I don't need your script, I'll try to write it myself

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 October 2006 - 11:37 AM

It's very easy, look at these functions:

For reading: file_get_contents()

For Writing: fopen(), fwrite(), fclose()

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 14 October 2006 - 12:55 PM

Is there a way of deleting what is in the current text file or overwriting it?
I have got it to write to the text file but how do I update what's in there or can't it be done?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 14 October 2006 - 06:17 PM

Yes, look at the second argument that fopen() accepts.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 dan245

dan245
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 October 2006 - 06:41 PM

To delete the contents of a text file try:

<?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!";

 }

}

?>

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

if ( fwrite ( $fp, "This will overwrite the text!" ) ) {

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


As for updating text try doing this:

<?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!";

 }

}

?>

-Dan M.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users