Jump to content

fwrite error


daktau

Recommended Posts

Hi All,

Does anyone know what is causing the error in this code?

 

<?
$errorLog_FilePath = "error_log.txt";
$handle = @fopen($errorLog_FilePath,'a+');
$strError = "hi!";
if(!fwrite($handle, $strError)){ //this is line 5
     echo('Cannot write to file');
}
?>

 

I get:

Parse error: syntax error, unexpected T_VARIABLE on line 5

 

I can't understand what is wrong with it.  :shrug:

 

thanks a lot,

George.

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/
Share on other sites

Hi daktau,

 

Your echo statement is using the incorrect syntax.  Change your code to read:

 

<?
$errorLog_FilePath = "error_log.txt";
$handle = @fopen($errorLog_FilePath,'a+');
$strError = "hi!";
if(!fwrite($handle, $strError)){ //this is line 5
     echo 'Cannot write to file';
}
?>

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-946989
Share on other sites

The posted code does not produce that error. It is likely that your actual code has something in it, like smart-quotes, that is causing php to see something that it does not expect in a php code file.

 

Are you using a programming editor to write code with?

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-946998
Share on other sites

<?
$errorLog_FilePath = "error_log.txt";
if($handle = @fopen($errorLog_FilePath,'a+')){
$strError = "hi!";
if(!fwrite($handle, 'hi')){
    	echo('Cannot write to file');
}
}
?>

 

This code produces this error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in filename.php on line 5

 

I use EditPlus v3.11 to edit php code. It does not add any of it's own stuff into my code.

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-947131
Share on other sites

No error. The code you just posted produces a error_log.txt file that contains the word hi.

 

Best guess is that whatever is in the file is being filtered out by the processing that it goes through when it is posted to and displayed on this forum.

 

It is probably the double quotes. Make sure you use the straight " and not any curly/smart quotes.

 

Edit: You could attach your actual file to a post, you could in fact have a broken php installation.

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-947224
Share on other sites

Thanks, I have changed the quotes to single and I'm saving the file as a utf-8 encoded script.

I'm still receiving the error:-

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in D:\rob-l\file_reNamerTEST2.php on line 5

 

I have attached the file this time. I'm fairly sure that my php installation is running ok as this is the first error of this sort I've ever seen. I was also running the script on a Mac with the php that comes with OSX and it still gave the same error.

 

Do you have any more clues?

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-947283
Share on other sites

Ok, can someone please run this on their system and try and explain why I'm still getting an error on line 5?

I have saved the file as ANSI encoded and included it with this post.

 

Can someone explain why if I save the same file with a different encoding it fouls up the php? eg. the server does not interpret the code. Even if I re-save the same file as a file encoding that works it seems to output the file as oriental lettering and does not convert the script.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-947771
Share on other sites

You still have funny characters. Some spaces (that should be ASCII 32) are ASCII 160.

 

Line 4, character 23 (The space right before the $strError)

Line 5, characters 4,5,6,7,20,26,29 (All spaces after the first 3)

 

Are you using a foreign keyboard or did you copy this code from somewhere else?

Link to comment
https://forums.phpfreaks.com/topic/179474-fwrite-error/#findComment-947987
Share on other sites

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.