Jump to content

Reading from a .txt file


MinusMyThoughts

Recommended Posts

I'm trying to make my site dynamically generate its content from a .txt file that my user can edit from a separate form......i thought a flat file would be better than a database because there's only one entry that needs to be overwritten every time the entry is edited. if you know a better method, i'm all ears......my question at hand, though, is this: how can i get the entered text to maintain its line breaks? i tried nl2br() with no luck. i can use HTML, but i want to assume that my user doesn't know any......as an additional problem, the file reads correctly, but then tacks on the character count of the entry at the end, like so: "TEST TEXT.10"here's my code:

<?php  $fp = fopen("home.txt", 'r');  $home= fpassthru($fp);  echo nl2br($home);?>

probably due to my own error, i could only get fpassthru() and readfile() to work. the nl2br() is still there for now, but i haven't been able to make it work. i left it in hopes of being told i placed it incorrectly and can solve all of my problems with a copy paste. :)...thanks!love,jason

Link to comment
Share on other sites

The fpassthru function writes directly to the output buffer (the browser), and returns the number of characters. So, $home contains only 10. That's why nl2br isn't working, because you're only using it on the number of characters. Try using file_get_contents instead:echo nl2br(file_get_contents("home.txt"));I would prefer a database though, because if you make an edit to something in the middle of your file, then you need to read the whole file, save everything up until the edited portion, save everything after the edited portion, and then write the first part, new part, and last part out to the file again. The database would just be an UPDATE statement. You could also store in an XML document, which would make the parsing and writing much easier.

Link to comment
Share on other sites

so instead of writing to a text file, i should just create the single-item database and allow the user to update that?...does the formatting work the same for that?...and will i need a unique identifier for a one-row table?...are these all dumb questions? :) i swear i'm starting to learn this stuff, if slowly...love,jason

Link to comment
Share on other sites

i took your advice and set up a database for my site. it's set up like this:(contentID, contentText)1, Home text.2, About text.3, Services text....there'll be more to it after i figure everything out. i've already run into a problem, though. i can't seem to update the database. something is wrong, but i'm not sure what. i'm not getting any errors, but i'm also not getting to the end of my script (my last 'echo' statement isn't working......do you see a problem straightaway?

<?php  $mysql_user = 'user';  $mysql_pass = 'pass';  $mysql_host = 'host';  $connect = mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die("Could not connect. " . mysql_error());  $db_select = mysql_select_db('database', $connect) or die("Could not select database. " . mysql_error());  echo 'Echo test.';  $contentUpdate = 'Web-uploaded content.';  $query = "update content set contentText = ".$contentUpdate." where contentID = 1";  $result = mysql_query($query);  if($result)	echo $contentUpdate;?>

...thanks so much for your help!love,jason

Link to comment
Share on other sites

You need to put your text values inside quotes, and also it's a good idea to escape the input to avoid SQL attacks:

$query = "update content set contentText = '".mysql_real_escape_string($contentUpdate)."' where contentID = 1";

Notice how I added the single quotes to enclose the text data.

Link to comment
Share on other sites

worked like a charm......thanks so much!...any tips for the database and/or interface? i think i've got it from here, but it seems like anything i can do, everyone on here can do faster and easier. :)love,jason

Link to comment
Share on other sites

unfortunately, i won't be able to use that......the only way that i can alter my databases (without spending extra money every month) is through the host's web-based copy of phpMyAdmin. it might actually be the same program......i'll survive. the only thing that sucks about it is the timeout for my login session. i get booted after ten minutes, so i end up logging in ten or eleven times......thanks for your help, guys!love,jason

Link to comment
Share on other sites

...i'll survive. the only thing that sucks about it is the timeout for my login session. i get booted after ten minutes, so i end up logging in ten or eleven times...
Yeah, I get that a lot on one of the server's I'm doing development on right now. With the way development typically goes, I'm in the database for a little while, then back in the code changing things around, then uploading and on the web side doing things, then check the database again and it kicks me out. Retarded paranoid admins.
Link to comment
Share on other sites

i don't need anything. i had asked for suggestions on better methods of dealing with my site, and phpMyAdmin was brought up. And my host is ... difficult, to say the least......it took me two separate phone calls and an hour and a half just to figure out that my package didn't allow C-Prompt access to my database. neither of the tech people i talked to were familiar with MySQL, and both of them had REALLY thick accents, so there was a lot of repetition and "Are you sure that NO ONE there knows how to use MySQL?"...long story short, i'm trying to circumvent the problem by turning a server i have lying around into a hosting service. but that's a whole other nightmare i'll have to deal with later on...love,jason

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...