Writing to a new textfile

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

Writing to a new textfile

Post by akimm »

My purpose of this program is to take a submitted article and write it to my server under /article/ with the name article and a number unique i did through a counter i built a while ago, I just reused the code and the variable visits adds an iterated number to the "/article/article" . $visits . ".txt" However, whenever I submit it gives me this code and take me to a http 404 which shouldn't be because I have the page as $_SERVER['PHP_SELF'] so if anyone can aid me I would appreciate it.

here is my latest version of the code

Code: Select all

<html>
<head>
<title>Adda-article</title>
<STYLE TYPE='text/css'>"
@import url(style.css);\n"; 
</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
$myfile = "count.txt";
//we assign our file name to the variable we'll use to handle it
if(file_exists($myfile))//if the file exists
    {                    //we run our counter script
    $var = fopen( $myfile,'r+');
    //opens in read and write mode our file
    $visits = fread($var,filesize($myfile));
    //puts the content of the file for its whole lenght
    rewind( $var );
    //resets the file position indicator to the beginning
    $visits++; //increments the actual number of vists by 1
    fwrite($var, $visits);
    //writes on the variable the actual (incremented) value
    fclose($var);//closes our file reference
    }
else
    {
        print "File $myfile doesn't exist...";
        Die();
//if the file doesn't exist prompts a warning and kills the script
    }
    $file = '/article/article' . $visits . '.txt';
if ($_POST['name'] == "") {
  echo "please provide a name";
}
if ($_POST['email'] == "") {
  echo "Please provide an email";
}
if ($_POST['article'] == "")  {
  echo "If you're submitting an article, you'd need the actual article.  Try again ";
} 
// 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 
// added 
if(file_exists($file))	{
  Die();
}	else {
  fopen($file, 'a') . fwrite($file, $user, 'w') . fclose($file);
}
 ?>
Post Reply