Jump to content


Photo

fwrite


  • Please log in to reply
11 replies to this topic

#1 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 April 2006 - 01:54 PM

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

2) The PHP script:
$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);
?>


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.
Think you're smarty?

(Gone until 20 to November)

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 04:06 PM

Does the web server have permission to write to your file?

Ken

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 April 2006 - 04:24 PM

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.
Think you're smarty?

(Gone until 20 to November)

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 April 2006 - 05:20 PM

[!--quoteo(post=364470:date=Apr 13 2006, 05:24 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Apr 13 2006, 05:24 PM) View Post[/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.

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 April 2006 - 06:22 PM

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.
Think you're smarty?

(Gone until 20 to November)

#6 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 13 April 2006 - 07:05 PM

CHMOD is a fancy term for the permissions that you give a file
got a problem? Google helps many of those in need

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 15 April 2006 - 03:32 PM

Ok, so I tried to do it this way:
$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";}
But this time I got 2 errors, the old one and error for the chmod() function too:
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
Is it the host?

Thanks again,
Orio.
Think you're smarty?

(Gone until 20 to November)

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 April 2006 - 03:42 PM

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.





#9 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 15 April 2006 - 03:51 PM

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.
Think you're smarty?

(Gone until 20 to November)

#10 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 April 2006 - 04:13 PM

*bump*
Anyone?

Orio.
Think you're smarty?

(Gone until 20 to November)

#11 cwncool

cwncool
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 16 April 2006 - 06:13 PM

This means that your webserver wont let you change the permissions. They can change them. Try emailing your webhost and ask them to change the permissions on 46.txt to 777 .

#12 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 April 2006 - 07:10 PM

Thanks alot to everyone :)

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users