Jump to content

Warning: fgets(): supplied argument is not a valid stream resource


thetwig

Recommended Posts

Hi guys, I'm doing a website for the condo and I'm having troubles with the new year party registration form. I think it's got something to do with my file handling. When I run it on my wamp server, it works fine. But when I upload it onto the internet it gives me a whole bunch of warnings:

Warning: fopen(newYearParty.txt) [function.fopen]: failed to open stream: Permission denied in /www/zymichost.com/i/n/d/inderasubangcondo/htdocs/newYrRegTq.php on line 18

 

Warning: fgets(): supplied argument is not a valid stream resource in /www/zymichost.com/i/n/d/inderasubangcondo/htdocs/newYrRegTq.php on line 19

 

Warning: fgets(): supplied argument is not a valid stream resource in /www/zymichost.com/i/n/d/inderasubangcondo/htdocs/newYrRegTq.php on line 20

 

Heres my code:

<?php
   $nam = $_POST["nam"];
   $block = $_POST["block"];
   $floor = $_POST["floor"];
   $house = $_POST["house"];
   $adults = $_POST["adults"];
   $children = $_POST["children"];
   $guests = $_POST["guests"];
   $nam = $_POST["nam"];
   
   global $fn, $ft;
   
   //set id and update total numbers
   $fn = fopen("newYearParty.txt","a+");
   $line1 = fgets($fn); $arr1 = explode(": ", $line1);   //adults
   $line2 = fgets($fn); $arr2 = explode(": ", $line2);   //children
   $line3 = fgets($fn); $arr3 = explode(": ", $line3);   //guests
   $line4 = fgets($fn); $arr4 = explode(": ", $line4);   //total
   $line4 = fgets($fn);   //blank line
   
   $numAdults = $arr1[1]+$adults;
   $numChildren = $arr2[1]+$children;
   $numGuests = $arr3[1]+$guests;
   $numTotal = $numAdults+$numChildren+$numGuests;

   $id = "nothing";
   while(!feof($fn))
   {   $line = fgets($fn);      //id
      $arr = explode(": ", $line);
      $id = $arr[1] + 1;      //get latest id and add 1 to it
      for($i=0; $i<6; $i++)
      {   $line = fgets($fn);
      }
   }
   
   fclose($fn);
   if($id == "nothing")
   {   $id = "0";
   }
   //end of set id
   
   //write to file
   $line0 = "\r\nId: ".$id."\r\n";
   $line1 = "Name: ".$nam."\r\n";
   $line2 = "Unit: ".$block."-".$floor."-".$house."\r\n";
   $line3 = "Adults: ".$adults."\r\n";
   $line4 = "Children: ".$children."\r\n";
   $line5 = "Guests: ".$guests."\r\n";
   
   $fn = fopen("newYearParty.txt","a+");
   fwrite($fn, $line0);
   fwrite($fn, $line1);
   fwrite($fn, $line2);
   fwrite($fn, $line3);
   fwrite($fn, $line4);
   fwrite($fn, $line5);
   fclose($fn);
   //end of write to file

   //write new numbers to file
   $ft = fopen("newYearPartyTemp.txt","a+");
   $lineA = "Adults: ".$numAdults."\r\n";
   $lineC = "Children: ".$numChildren."\r\n";
   $lineG = "Guests: ".$numGuests."\r\n";
   $lineT = "Total: ".$numTotal."\r\n\r\n";
   
   fwrite($ft, $lineA);
   fwrite($ft, $lineC);
   fwrite($ft, $lineG);
   fwrite($ft, $lineT);
   
   $fn = fopen("newYearParty.txt","a+");
   for($i=0; $i<5; $i++)
   {   $line = fgets($fn);
   }
   while(!feof($fn))
   {   $line = fgets($fn);
      fwrite($ft, $line);
   }
   
   fclose($fn);
   fclose($ft);
   unlink("newYearParty.txt");      //delete newYearParty.txt
   rename("newYearPartyTemp.txt","newYearParty.txt");   //rename newYearPartyTemp.txt to newYearParty.txt   
   //end of write new numbers to file
   
?>

 

Any ideas on how to fix this up? Thanks a lot in advance.

Link to comment
Share on other sites

And why do I need permission to write to a file?

 

Srsly?

 

So is there any way I can get permission?

 

CHMOD your file to 755, if that doesn't work then 777 will.

Explained in more detail here:

http://www.pageresource.com/cgirec/chmod.htm

 

On a side note, while databases can be daunting at first, I think it may be much more suitable for your situation. Have you considered implementing a database rather than using a text file for all of it?

Link to comment
Share on other sites

hey Zurev.. I came up with this error:

Parse error: syntax error, unexpected T_LNUMBER in /www/zymichost.com/i/n/d/inderasubangcondo/htdocs/newYrRegTq.php on line 14

 

I tried putting that line in, and since I don't really understand it I tried 4 combinations of what I thought it could be changing the .cgi to .txt and the 755 to 777..

1. Telnet/cgi-bin>chmod 755 newYearParty.cgi

2. Telnet/cgi-bin>chmod 777 newYearParty.cgi

3. Telnet/cgi-bin>chmod 755 newYearParty.txt

4. Telnet/cgi-bin>chmod 777 newYearParty.txt

 

All four didn't work... here's where i put it.. (line 14)

<?php
$nam = $_POST["nam"];
$block = $_POST["block"];
$floor = $_POST["floor"];
$house = $_POST["house"];
$adults = $_POST["adults"];
$children = $_POST["children"];
$guests = $_POST["guests"];

Telnet/cgi-bin>chmod 777 newYearParty.cgi	//I PUT IT HERE

//set id and update total numbers
$fn = fopen("newYearParty.txt","a+");
$line1 = fgets($fn); $arr1 = explode(": ", $line1);	//adults
$line2 = fgets($fn); $arr2 = explode(": ", $line2);	//children
$line3 = fgets($fn); $arr3 = explode(": ", $line3);	//guests
$line4 = fgets($fn); $arr4 = explode(": ", $line4);	//total
$line4 = fgets($fn);	//blank line

$numAdults = $arr1[1]+$adults;
$numChildren = $arr2[1]+$children;
$numGuests = $arr3[1]+$guests;
$numTotal = $numAdults+$numChildren+$numGuests;

$id = "nothing";
while(!feof($fn))
{	$line = fgets($fn);		//id
	$arr = explode(": ", $line);
	$id = $arr[1] + 1;		//get latest id and add 1 to it
	for($i=0; $i<6; $i++)
	{	$line = fgets($fn);
	}
}

fclose($fn);
if($id == "nothing")
{	$id = "0";
}
//end of set id

//write to file
$line0 = "\r\nId: ".$id."\r\n";
$line1 = "Name: ".$nam."\r\n";
$line2 = "Unit: ".$block."-".$floor."-".$house."\r\n";
$line3 = "Adults: ".$adults."\r\n";
$line4 = "Children: ".$children."\r\n";
$line5 = "Guests: ".$guests."\r\n";

$fn = fopen("newYearParty.txt","a+");
fwrite($fn, $line0);
fwrite($fn, $line1);
fwrite($fn, $line2);
fwrite($fn, $line3);
fwrite($fn, $line4);
fwrite($fn, $line5);
fclose($fn);
//end of write to file

//write new numbers to file
$ft = fopen("newYearPartyTemp.txt","a+");
$lineA = "Adults: ".$numAdults."\r\n";
$lineC = "Children: ".$numChildren."\r\n";
$lineG = "Guests: ".$numGuests."\r\n";
$lineT = "Total: ".$numTotal."\r\n\r\n";

fwrite($ft, $lineA);
fwrite($ft, $lineC);
fwrite($ft, $lineG);
fwrite($ft, $lineT);

$fn = fopen("newYearParty.txt","a+");
for($i=0; $i<5; $i++)
{	$line = fgets($fn);
}
while(!feof($fn))
{	$line = fgets($fn);
	fwrite($ft, $line);
}

fclose($fn);
fclose($ft);
unlink("newYearParty.txt");		//delete newYearParty.txt
rename("newYearPartyTemp.txt","newYearParty.txt");	//rename newYearPartyTemp.txt to newYearParty.txt	
//end of write new numbers to file

?>

 

Let me know what I'm doing wrong.  Thanks for your help.

 

Link to comment
Share on other sites

hey Zurev.. I came up with this error:

Parse error: syntax error, unexpected T_LNUMBER in /www/zymichost.com/i/n/d/inderasubangcondo/htdocs/newYrRegTq.php on line 14

 

I tried putting that line in, and since I don't really understand it I tried 4 combinations of what I thought it could be changing the .cgi to .txt and the 755 to 777..

1. Telnet/cgi-bin>chmod 755 newYearParty.cgi

2. Telnet/cgi-bin>chmod 777 newYearParty.cgi

3. Telnet/cgi-bin>chmod 755 newYearParty.txt

4. Telnet/cgi-bin>chmod 777 newYearParty.txt

 

All four didn't work... here's where i put it.. (line 14)

<?php
$nam = $_POST["nam"];
$block = $_POST["block"];
$floor = $_POST["floor"];
$house = $_POST["house"];
$adults = $_POST["adults"];
$children = $_POST["children"];
$guests = $_POST["guests"];

Telnet/cgi-bin>chmod 777 newYearParty.cgi	//I PUT IT HERE

//set id and update total numbers
$fn = fopen("newYearParty.txt","a+");
$line1 = fgets($fn); $arr1 = explode(": ", $line1);	//adults
$line2 = fgets($fn); $arr2 = explode(": ", $line2);	//children
$line3 = fgets($fn); $arr3 = explode(": ", $line3);	//guests
$line4 = fgets($fn); $arr4 = explode(": ", $line4);	//total
$line4 = fgets($fn);	//blank line

$numAdults = $arr1[1]+$adults;
$numChildren = $arr2[1]+$children;
$numGuests = $arr3[1]+$guests;
$numTotal = $numAdults+$numChildren+$numGuests;

$id = "nothing";
while(!feof($fn))
{	$line = fgets($fn);		//id
	$arr = explode(": ", $line);
	$id = $arr[1] + 1;		//get latest id and add 1 to it
	for($i=0; $i<6; $i++)
	{	$line = fgets($fn);
	}
}

fclose($fn);
if($id == "nothing")
{	$id = "0";
}
//end of set id

//write to file
$line0 = "\r\nId: ".$id."\r\n";
$line1 = "Name: ".$nam."\r\n";
$line2 = "Unit: ".$block."-".$floor."-".$house."\r\n";
$line3 = "Adults: ".$adults."\r\n";
$line4 = "Children: ".$children."\r\n";
$line5 = "Guests: ".$guests."\r\n";

$fn = fopen("newYearParty.txt","a+");
fwrite($fn, $line0);
fwrite($fn, $line1);
fwrite($fn, $line2);
fwrite($fn, $line3);
fwrite($fn, $line4);
fwrite($fn, $line5);
fclose($fn);
//end of write to file

//write new numbers to file
$ft = fopen("newYearPartyTemp.txt","a+");
$lineA = "Adults: ".$numAdults."\r\n";
$lineC = "Children: ".$numChildren."\r\n";
$lineG = "Guests: ".$numGuests."\r\n";
$lineT = "Total: ".$numTotal."\r\n\r\n";

fwrite($ft, $lineA);
fwrite($ft, $lineC);
fwrite($ft, $lineG);
fwrite($ft, $lineT);

$fn = fopen("newYearParty.txt","a+");
for($i=0; $i<5; $i++)
{	$line = fgets($fn);
}
while(!feof($fn))
{	$line = fgets($fn);
	fwrite($ft, $line);
}

fclose($fn);
fclose($ft);
unlink("newYearParty.txt");		//delete newYearParty.txt
rename("newYearPartyTemp.txt","newYearParty.txt");	//rename newYearPartyTemp.txt to newYearParty.txt	
//end of write new numbers to file

?>

 

Let me know what I'm doing wrong.  Thanks for your help.

 

I'm afraid this is my fault, I meant to give you that page so you could understand the concept, that isn't a line meant for PHP, not in that context anyway.

 

CHMOD by right clicking the file in your ftp client and see from there, it's changing the mode, however, an easier bet for you is to use php's chmod function.

http://php.net/manual/en/function.chmod.php

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.