Page 1 of 1

Write form data into a text file

Posted: Tue Nov 11, 2008 4:28 pm
by hotblooded
I am currently having problems writing data inputted to a form into a .txt file. I've looked a couple different places, all of which seem to provide the same code, and it just isn't working for me. In fact, it doesn't even seem to be touching the text file. Here's the code:

Code: Select all

<?php
 
$filename = "current_semester.txt";
$sem = $_POST['Semester'];
$year = $_POST['Year']+2000;
 
$fp = fopen($filename, 'a') or die("can't open file");
fwrite($fp, $sem);
fwrite($fp, $year);
fclose($fp);
 
?>
Any help is greatly appreciated!

Thanks in advance...

Re: Write form data into a text file

Posted: Tue Nov 11, 2008 4:30 pm
by hotblooded
Meant to post this in the "code" forum...any way I can move it?

Re: Write form data into a text file

Posted: Wed Nov 12, 2008 5:07 am
by novice4eva
Your code is perfectly correct, the problem may lie in permission. Make sure that the folder you are creating/appending the file has the read/write permission. Just to check you can go through functions like is_readable,is_writeable.

Re: Write form data into a text file

Posted: Wed Nov 12, 2008 10:44 am
by hotblooded
I tried both suggestions and neither one seemed to work. Let me know if something's wrong with my code but I think I got it right.

Code: Select all

<?php
 
$filename = "current_semester.txt";
$sem = $_POST['Semester'];
$year = $_POST['Year']+2000;
 
$somecontent = $sem." ".$year ;
 
if(is_writable($filename))
{
$fp = fopen($filename, 'a') or die("can't open file");
fwrite($fp, $somecontent);
fclose($fp);
echo "file written";
}
else {
echo "file not writable";
}
?>
Thanks in advance.

Re: Write form data into a text file

Posted: Wed Nov 12, 2008 11:23 pm
by novice4eva
Have you enabled error reporting?? Put these lines at the top and tell us what to get

Code: Select all

 
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
 

Re: Write form data into a text file

Posted: Sat Nov 15, 2008 11:13 am
by hotblooded
I put the code at the top of the php statement and when I first opened the page, this is what I got:

Notice: Undefined index: Semester in C:\wamp\www\newland\sem_select.php on line 37

Notice: Undefined index: Year in C:\wamp\www\newland\sem_select.php on line 38
file readfile written

I'm assuming this means that the error code is working as I have the form on the same page and those variables aren't defined at first. After I submit the form, these errors go away and it still doesn't write to the text file.

I don't understand why it isn't writing to the file.

Any ideas? I'm stumped...

Re: Write form data into a text file

Posted: Sat Nov 15, 2008 3:32 pm
by califdon
The first thing to check is whether there are any values in your variables. If there are not, of course it won't add anything to the file. You can check this by echoing the variables to the screen just before (or instead of) trying to write them to the file. You could also try to write a defined string to the file. If it writes "hello" okay but won't write your variables, then you know that your problem is not in your syntax or permissions, but in your variables. If it won't write "hello" to the file, then you know that your problem must be file permissions or something like that. Always try to narrow down the area that is causing the problem, by eliminating possibilities.

Re: Write form data into a text file

Posted: Mon Nov 17, 2008 1:35 pm
by hotblooded
I have actually tried to echo the values in the variables and they echo what the form submits properly. I also tried just writing static text ("hello") into the file and it didn't write it properly. As you said, this means it must have something to do with permissions. When I go to the file in Windows Explorer and go to its properties, "Read Only" is unchecked. However, when I got up one level in the folder structure, the folder that contains the text file is read only; as is the folder above that one, and the one above that. When I try to uncheck the read only checkbox, it seems to work as it goes through a sequence, as if to say that it is working. When i close the properties dialog and reopen it, read only is checked again. Is there any way to get these folders to not be read only? Also, is this an issue, considering the text file is contained in the same directory as the pages from which the .txt file is being written?

Thanks...

Re: Write form data into a text file

Posted: Mon Nov 17, 2008 6:22 pm
by califdon
hotblooded wrote:Notice: Undefined index: Semester in C:\wamp\www\newland\sem_select.php on line 37

Notice: Undefined index: Year in C:\wamp\www\newland\sem_select.php on line 38
file readfile written
Are you still generating those Notices? To me, that says that the Semester and Year values are NOT being passed in the $_POST array. Yet you say that you have printed out the variables and they are there? That's a disconnect for me. I don't know how both those things can be true at the same time.

Re: Write form data into a text file

Posted: Tue Nov 18, 2008 8:58 am
by hotblooded
Yes, I get these errors when I first open the page (i.e. before anything on the form is submitted). After I fill out the form and submit it, these errors go away because the variables then have a value. Hope this clears things up...

Thanks...

Re: Write form data into a text file

Posted: Tue Nov 18, 2008 9:40 am
by hotblooded
Now I uploaded it to a LAMP server (I'm using Vista) and it works just fine. This said, I believe it's not working on my machine because of the file permissions. The text file is not marked as read only; however, the directory it is in is read only. My issues are described better in one of my above posts. Let me know if these file permissions can be changed.


Thanks...

Re: Write form data into a text file

Posted: Tue Nov 18, 2008 11:07 am
by califdon
hotblooded wrote:... (I'm using Vista) ...
Oops! That could be the problem. Vista likes to take control and "hide" files ("to make for a better user experience", in Microsoft's terminology), which is why so many people, including me, have refused to install it. But that doesn't help you with your problem. Hopefully, someone here will have some experience with using Vista and be able to give you some advice.

Re: Write form data into a text file

Posted: Tue Nov 18, 2008 3:09 pm
by hotblooded
Yeah it does get annoying sometimes...anyone else have any thoughts? Thanks for helping me figure out what the problem was...now I just have to get it to write properly...

I've also tried reading the file in another page and it isn't reading it right either...

Any ideas?