writing to a file through php

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
mrkkhattak
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 3:01 am
Location: Pakistan

writing to a file through php

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
mrkkhattak
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 3:01 am
Location: Pakistan

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have you tried $myFile = fopen("data.txt", "a+") ?
mrkkhattak
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 3:01 am
Location: Pakistan

Post 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
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Are you getting any sort of error message?
mrkkhattak
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 3:01 am
Location: Pakistan

Post 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
southeastweb
Forum Newbie
Posts: 14
Joined: Sun Mar 21, 2004 3:28 am
Location: Florida

send over your script

Post by southeastweb »

reply with your script. I'll see if I can debug it.
mrkkhattak
Forum Newbie
Posts: 5
Joined: Thu Mar 18, 2004 3:01 am
Location: Pakistan

Post by mrkkhattak »

i have posted the script in my first post.

Thank you in advance !

Regards,

-Meraj
Post Reply