Posting to a text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reh
Forum Newbie
Posts: 9
Joined: Tue Aug 17, 2004 10:35 am

Posting to a text file

Post by reh »

Hello there,

I am currently trying to create a blog, but instead of posting the information to a database, i am posting information to a text file (not my idea). When i am posting regular text information i have no problems, but the flow starts to get a bit funky when i try to post an image name to the text file.

I have an image uploade page that submits an image to a folder on my server, and at the same time posts the image name to an append page that together with information from text feilds is posted to the text file.
This text file will then enable me to display inputted information and display the image by surrounding the image name pulled from the text file with <img src... tags.

As anyone who has worked with displaying information from text files the information in the text file can not have any spaces or empty rows, because the display features are depenedent on the format of the text file.
My problem is that when the information about the image is posted it creates a missed line in the text file, but the regular text information does not create any spaces.

I have probably confused myself even more with this explanation attempt, so if anyone does not understand my problem please let me know.

Thanks to anyone who takes the time to read this post.

I Reh

P.s let me know if you would like to see the code that i have used
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Yeah, drop your code in (only the relevent parts, if possible).
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

use xml style tags in your data file to maintain data integrity.

like this:

Code: Select all

<date>8/17/04</date>
<title>Tuesday's entry</title>
<image_name>test.gif</image_name>
Grab an existing xml parser if you are not sure how to read the data file for recall.
reh
Forum Newbie
Posts: 9
Joined: Tue Aug 17, 2004 10:35 am

Post by reh »

Hello there,

I was trying to avoid posting the code on here because the page structure is very messy. So before you read the code i think i should explain how the pages link to each other so that you are not wasting your time trying to understand what i have done.

(although i have used around 5 pages, if anybody has any suggestions to move the code into a smaller number of files or even one file then please feel free to let me know.)

How the page structure flows is the first page is a text input with a file upload option, this then links to a php page that uploads the selected image file and prints out the details of the file, this file also posts the image name to the next page, which is a page to add text to the image submission. Then this page posts the image name and the inputted text to an append file which posts all details to the text file.

code for the third page that deals with the upload of the image and the posting of the image name.

Code: Select all

<?php
if ($file !="")
&#123;
copy ("$file", "/home/mza11/networldnetworks-www/drafts/irfan/php/portfolio/pics/$file_name")
or die ("could not copy file");

&#125;
else &#123; die ("No file specified");       
 &#125;
?>

<html><head><title>upload complete</title> </head>

<body> <h3>File upload suceeded...</h3>
<ul>
<li>Sent: <?php echo "$file_name"; ?>
<li>Size: <?php echo "$file_size";?> bytes
<li>Type: <?php echo "$file_type"; ?>
</ul>
<img src="<?php echo "pics/$file_name";?>">

<br>
<form name="paste" action="filentext2.htm" method="POST">
<input type="hidden" name="file_name" value='
<?php echo "$file_name";?>'>
<input type="submit" value="paste image">

</form>
</body>
</html>
Code for page that deals with posting image name and adding text for the text file.

Code: Select all

content2 ="$file_name" 

<html><head><title>your favourites</title></head>
<body>

<form action="appendtofile.php" method="post">

<input type="text" size"100" name="content"><br>
 <br>

<input type="text" size"100" name="content1"><br>
 <br>

<input type="hidden" name="content2" value="
<?php echo "$file_name";?>">
<input type="submit" value="Submit this form">
<br>
</form>


file:<?php echo "$file_name"; ?>&nbsp;to be uploaded

<br>
<BR>
<a href= uploader.htm> Upload another file</a>;
</body></html>
Code for page that deals with posting all details to the txt file.

Code: Select all

<?php

$string = "$content";
$string1= "$content1";
$string2= "$content2";


$fp = fopen("/home/mza11/networldnetworks-www/drafts/irfan/php/readtext.txt","a"); 
if(!$fp) &#123;
   print "error! The file could not be opened";
   exit; 
&#125; 
$stringtowrite=$string."|".$string1."|".$string2;
fwrite($fp, $stringtowrite); 
fclose($fp);
?>
Thanks a bunch to the people reading and contributing to this post.
I Reh

(To xisle)could you please eloborate on the xml suggestions that you gave me as i have not got a clue about xml. cheers.
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

I can only confirm what xisle already mentioned. You should get some basic information about what XML is (http://www.w3schools.com) and use that. There is a bunch of xmlparsers for PHP. This is what XML was made for. You can call it a plain text database since it is structured and you can get any entry you want.

Be sure that you use sth. like htmlspecialchars on the text you want to store so no conflicts with the structure of the XML file appear.
reh
Forum Newbie
Posts: 9
Joined: Tue Aug 17, 2004 10:35 am

Post by reh »

So what you are saying is that the problems that i am having is due to not using xml.

Is the code ok though, and can i compress the code to fit into less pages or files.

thanks
I Reh
Post Reply