Jump to content

How can I get fopen to work or write to a file someother way?


ahvceo

Recommended Posts

Hi All,

 

I am 72 and just starting to learn php.  I am having a problem with the "$ref = $HTTP_COOKIE_VARS["ref"];" statement so I tried to see what was being returned in $ref.  I see nothing if I do an "echo" (I never have seen echo output anything I could see!) so I thought I would write $ref out to a file.  The server pukes at the fopen statement at line 57 (where the fopen statement is) with the following error message:

"

Parse error: syntax error, unexpected T_IF in /home/ahventur/public_html/user/cookie.php on line 57"

 

I copied and pasted the code for writing to a file directly from a tutorial and I have checked several other sources and all say the same thing.  The code is right I think.  Can someone tell me how to get at the $ref contents so I can see what is being returned by $HTTP_COOKIE_VARS["ref"].  I would like to be able to use the fopen and fwrite functions for future reference.  Any help would be appreciated greatly.

 

Thanks

ahvceo

 

code follows:

 

$ref = $HTTP_COOKIE_VARS["ref"];

echo $ref;

$f = xyz.txt

if (!$handle = fopen($f, 'w')) {

echo "Cannot open file ($f)";

exit;

}    

if (fwrite($handle, $ref) === FALSE) {

echo "Cannot write to file ($filename)";

exit;

}

 

Very inspiring thread, at 72! thats fantastic, hopefully you will in no time become a great programmer.

 

when you usually have a unexpected something, syntax error, go to the lines before line 57" and see if you have a ";" after each line of code.

 

and as Ken mentioned, fopen args for the file name has to be quoted

Hi Guys,

 

Thanks for all the good cheer!  The only thing being wrong with being 72 is your memory isn't as good and you have trouble with tiny details.  There was one other thing that I lost some time ago but I can't remember what it was.

 

Well the server is no longer puking on my code thanks to your help.  I run the code with no errors.  The only problem is I cannot find the file!  Where in the XXXX does it go?

 

I have changed the code slightly as follows

 

$ref = $HTTP_COOKIE_VARS["ref"];

echo $ref;

$f = 'xyz.txt';

$today = date("F j, Y, g:i a");                // March 10, 2001, 5:16 pm

 

if (!$handle = fopen($f, 'w')) {

echo "Cannot open file ($f)";

exit;

}

if (fwrite($handle, $ref) === FALSE) {

echo "Cannot write to file ($filename)";

exit;

}

if (fwrite($handle, $today) === FALSE) {

echo "Cannot write ($today)";

exit;

}

fclose($handle);

 

I added a write time statement so I would know if the file was really being written when I though it should be and I added a fclose statement because I remember form somewhere that you need to close a file before it really wirite it out.

 

I also tried to put a path in the file namer(C:\xyz.txt) to see if I could find it on my C drive,  No Luck.

 

Any ideas where the file might be going of how I find out if it is being written at all?

 

Thanks

ahvceo

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.