Jump to content

fwrite


Orio

Recommended Posts

Hello,

I have some problems using the fwrite function.
What basicly I want to do is insert information that someone sent (with a form) to a txt file.
The pages:

1) The HTML page (form):
[code]
<form method=post action="form.php">
<b>Your name:</b><br>
<input type="text" name="Name" size="30"><br><br><br>
<b>How would you rate?</b><br><br>
1<input type=radio name="Rating" value="1">   
2<input type=radio name="Rating" value="2">   
3<input type=radio name="Rating" value="3">   
<br><br><br>
<b>Comments:</b>
<br>
<textarea cols="40" rows="10" name="Comments"></textarea>
<input type="submit" value="Send">
<input type="reset" value="Clear the form">
</form>
[/code]

2) The PHP script:
[code]
$filename = '46.txt';
$content = "Name: ".$_POST['Name']." \r\n Rating: ".$_POST['Rating']." \r\n Comments: \r\n ".$_POST['Comments']." \r\n ".$ip." \r\n ------ \r\n \r\n";

if (!$handle = fopen($filename, 'ab'))
{       echo "There was a problem opening the file";
         exit;
}

if (fwrite($handle, $content) === FALSE)
{
       echo "Cannot write to file";
       exit;
}
  
echo "Thank you!";
fclose($handle);
?>
[/code]


But when I submit, I get the following error:
Warning: fopen(46.txt): failed to open stream: Permission denied in C:\Inetpub\vhosts\oriosriddle.com\httpdocs\form.php on line 8.

Can you help me? I cant find what the problem is.

I am hosted in serversea.com, and I dont have any control on the apache or php files. The server runs on Windows.

I know I can do it with mysql, but I dont know mysql =/ And its pretty urgent.

Thanks,
Orio.
Link to comment
Share on other sites

[!--quoteo(post=364470:date=Apr 13 2006, 05:24 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Apr 13 2006, 05:24 PM) [snapback]364470[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I dont know...
Is there a way knowing by using phpinfo() or something?

EDIT- just went over the info phpinfo() gives, and I cant find anything about writing to files.

Orio.
[/quote]
What Kan ment was does your file 46.txt have the correct CHMOD settings set, this is nothing to do with how PHP is setup but the permissions on the file(s) you are writting data to.
Link to comment
Share on other sites

Oh, its a normal txt file I created on my computer, and I checked if its writable with the is_writable() function, and it is... Still no idea whats the problem.
By the way, I have no idea what CHMOD is... lol

Thanks again,
Orio.
Link to comment
Share on other sites

Ok, so I tried to do it this way:
[code]
$filename = '46.txt';
$somecontent = "Name: ".$_POST['Name']." \r\n Rating: ".$_POST['Rating']." \r\n Comments: \r\n ".$_POST['Comments']." \r\n ".$ip." \r\n ------ \r\n \r\n";
chmod("46.txt", 0644);

if (is_writable($filename))
{
   if (!$handle = fopen($filename, 'ab'))
  {
         echo "There was a problem opening the file";
         exit;
   }


   if (fwrite($handle, $somecontent) === FALSE)
  {
       echo "Cannot write to file";
       exit;
   }

   echo "Thank you!";
   fclose($handle);
}

else
{echo "The file is not writable";}
[/code]
But this time I got 2 errors, the old one and error for the chmod() function too:
[code]
Warning: chmod(): Permission denied in C:\Inetpub\vhosts\oriosriddle.com\httpdocs\save.php on line 5

Warning: fopen(46.txt): failed to open stream: Permission denied in C:\Inetpub\vhosts\oriosriddle.com\httpdocs\save.php on line 9
[/code]
Is it the host?

Thanks again,
Orio.
Link to comment
Share on other sites

0644 only allows to read the file. You cannot edit the file with that CHMOD value. To be able to edit a file you'll need to give the file a CHMOD value of 0777.

Now I don't think chnaging the permissions of the file 0777 will affect the file much as you are on Windows using IIS. Windows doesn't have CHMOD. Only *unix and Macs have CHMOD. So I'd strip out your chmod function as it isn't going to do anything.

My guess is that the file you are trying to edit is read-only (this you can check in your FTP Client by right clicking 46.txt and selecting properties and making sure the Read-only checkbox is unticked) or your webhost may be disabling the use of fopen.



Link to comment
Share on other sites

I checked on my FTP the premission of the file, and in the place where the "Read-Only" check box is supposed to be there's this message:
"This server does not support changing file premissions".
Does this mean its a read only and I cant change it? But hows that possible if the is_writable() function didn't return FALSE?

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.