Page 1 of 1

writing to a file through php

Posted: Thu Mar 18, 2004 3:01 am
by mrkkhattak
Hello,

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.

Code: Select all

<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>


Thank you in advance.

Regards,

-Meraj

Posted: Thu Mar 18, 2004 3:12 am
by patrikG
Have you checked that the directory in which your file is located has the right permissions? If in doubt, chmod it to 0777.

Posted: Thu Mar 18, 2004 3:33 am
by mrkkhattak
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.

Code: Select all

<?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.

-Meraj

Posted: Thu Mar 18, 2004 3:36 am
by patrikG
Have you tried $myFile = fopen("data.txt", "a+") ?

Posted: Thu Mar 18, 2004 3:51 am
by mrkkhattak
I tried it & it works just fine for later post.

Code: Select all

$myFile = fopen("data.txt", "a+");
But when I do it for my previous post it doesn't work.

Code: Select all

$fd = fopen("data.txt", "a+");
-Meraj

Posted: Thu Mar 18, 2004 7:03 am
by coreycollins
Are you getting any sort of error message?

Posted: Sun Mar 21, 2004 12:52 am
by mrkkhattak
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).

Regards,

-Meraj

send over your script

Posted: Sun Mar 21, 2004 3:28 am
by southeastweb
reply with your script. I'll see if I can debug it.

Posted: Wed Mar 24, 2004 1:52 am
by mrkkhattak
i have posted the script in my first post.

Thank you in advance !

Regards,

-Meraj