Flat file question

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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Flat file question

Post by akimm »

I have received from plenty of reputable people on this site, people whom know more than I'll ever know. Still this code remains with errors, perhaps I have a misunderstanding, I thought* that I could write textfiles with php, for instance if user submits information $info I can write this $info to one seperate textfile on a specific directory. If i'm correct on this then I will continue to try to make the corrections which have been pointed out for me.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Correct.

Code: Select all

$filename = '/path/to/file.txt';

$info = $_POST['info'];

$handle = fopen($filename, "w");

fwrite($handle, $info);

fclose($handle);

echo file_get_contents($filename);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Then would it be possible for you to look at this code

Post by akimm »

Feyd and a few others had looked at it, I think I now have it correct, but still it says some strange error I've never seen from any script i've made yet it says

"parse error unexpected $end"

I will post the code if you'll look at it real quick to see if i'm making a mistake.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Post the code then...
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok

Post by akimm »

Just to not confuse cuz i'm new at coding so my purpose my be unclear.

I want $_POST['name'] $_POST['email'] and $_POST['article']
all written to a textfile. Each submission I want a number added to it, like article1.txt article2.txt and so on, so that each article added is unique. If this is still possible please someone tell me where I'm screwing up at.

Code: Select all

<?php 
echo "<html>\n"; 
echo"<head>\n"; 
?>
<STYLE TYPE='text/css'>
@import url(style.css) 
</STYLE>
<body>
<?php 
$date = date('l n/d/y g:i a'); 
echo $date; 
?> 
<form action='<?php $_SERVER['PHP_SELF'] ?> method='POST'>
<p><b>Your name</b> 
<input type='text' name='name' size='30'></p>
<p><b>Your email</b>
<input type='text' name='email' size='30'></p>
<p><b>article</b>
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P> 
<p><input type='submit' name='submit' value='submit your aritlce'></p> 

<?php 
if ($_POST['name'] == "" && $_POST['email'] == "" && $_POST['article'] == "") { 
   echo "Please fill out all form fields"; 
} 

// recognize what i want from user 
// variable to concocatanate to append user info 
$user = $_POST['name'] . $_POST['email'] . $_POST['article']; 

// start an iteration that will add 1,2,3,4,5,6 and so on to each article 
 
$ext = '.txt'; 
for ($i=0; $i<count($i); $i++) { 
  $fileName = "article/article". $i . $ext; 
} 
$fp = fopen($fileName . $user . 'a'); 
$fw = fwrite($fileName, $user, 'w'); 
$fc = fclose($fileName); 

if (!$user) { 
  echo "You need to fill out all form fields"; 
} else { 
   $fp . $fw . $fc; 
// THE ERROR IS HERE 
}        
?>
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

This line is missing a closing quote (after the closing ?>), and if you notice all the syntax coloring goes to hell immediately after. Not to mention you actually don't 'do' anything there, you don't use print or echo to display the value of $_SERVER['PHP_SELF'].

Code: Select all

<form action='<?php $_SERVER['PHP_SELF'] ?> method='POST'>
should be

Code: Select all

<form action='<?php echo {$_SERVER['PHP_SELF']}; ?>' method='POST'>

There may be other issues in the script, but start with that.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

are you sure

Post by akimm »

My editor said there was an unexpected curly bracer I don't think I need to use the echo either, I never have in any other scripts i've written.
Post Reply