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!
I have this small php script through which I am trying to write in a file. It runs fine but I am unable to write to the required file. Pls do let me know what I am doing wrong ? I tried to search the forum but I was getting about 240+ pages result, I did check the first 10-15 pages but couldn't solve my problem.
This file "data.txt" is located from where I am running this script. I have given this file 777 persmission but it is unable to write to it.
<html>
<head>
<title>form data</title>
</head>
<body bgcolor="#ffffff">
<?php
//create the form
$form = "
<form action="file_store.php" method="post">
<input type="hidden" name="seenform" value="y">
your name :
<input type="text" name="name" size="20" maxlength="20" value=""><br>
your email :
<input type="text" name="email" size="20" maxlength="20" value=""><br>
your prefered language :
<select name="language">
<option value="">choose a language:
<option value="english">english
<option value="urdu">urdu
</select><br>
your occupation :
<select name="job">
<option value="">choose a job:
<option value="student">student
<option value="programmer">programmer
</select><br>
<input type="submit" value="submit">
</form>";
// has the form already been filled
if ($seenform != "y") :
print "$form";
else :
$fd = fopen("data.txt", "a");
// make sure the user has not inserted any bar in the fields
$name = str_replace("|", "", $name);
$email = str_replace("|", "", $email);
// assemble user information
$user_row = $name."|".$email."|".$language."|".$job."\n";
fwrite($fd, $user_row) or die ("Couldn't write to file!");
fclose($fd);
print "print thank you for ur input";
endif;
?>
</body>
</html>
Yes. I had that thing in mind so I did make it 777 but still not working. One thing more when I wrote the following code, this was able to write to that file.
<?php
// open file for writing
$myFile = fopen("data.txt", "w");
// make sure the open was successful
if(!($myFile))
{
print("file couldn't be opened");
exit;
}
for($index=0; $index<10; $index++)
{
// write a line to the file
fwrite($myFile, "line $index\n");
}
// close the file
fclose($myFile);
?>
As I run this script, it adds data to my file, but through that form (my previous post) i am not able to add data to that file.
No. I am not getting any sort of error message. The script executs just fine but only not writing to the file. (even a friend of mine tried this on windows machine & he told me that it is working).