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;

}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

72. I'm very impressed. It's very refreshing to see someone of your age to be interested in new ideas and willing to open his mind and learn new things.

 

I wish you the best of luck with your PHP programming!

Link to comment
Share on other sites

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

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.