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
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
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
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
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
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');
}
}
?>

 

Try editing that file in notepad to remove those funny characters.

Link to comment
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
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
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.