strawbshaker Posted August 7, 2006 Share Posted August 7, 2006 Hi there,I'm using the following bit of code and get the error message:[color=red]Parse error: parse error, unexpected '{' in /xxx/file.php on line 4[/color][code]<?php$file = "testpage.txt";if (file_exists ($file)){try { // The Error is occuring hereif ($readfile = fopen ($file, "r")){$curvalue = fread ($readfile,filesize($file));$curvalue++;if (is_writable ($file)){try {if ($writefile = fopen ($file, "w")){fwrite ($writefile, $curvalue);echo "Wrote $curvalue to file.";} else {throw new exception ("Sorry, the file could not be opened");}} catch (exception $e){echo $e->getmessage();}} else {echo "File could not be opened for writing";}} else {throw new exception ("Sorry, the file could not be opened.");}} catch (exception $e) {echo $e->getmessage();}} else {echo "File does not exist.";}?>[/code]Can anyone see the problem? Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/ Share on other sites More sharing options...
ronverdonk Posted August 7, 2006 Share Posted August 7, 2006 I can even run it with my own text file. So: no, don't see the problem. Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/#findComment-70770 Share on other sites More sharing options...
wildteen88 Posted August 7, 2006 Share Posted August 7, 2006 [b]edit[/b] I made a complete tit of myself.Going through your code and indenting it I found your { and } brakets where unbalanced. Try this:[code=php:0]<?php$file = "testpage.txt";if (file_exists ($file)){ // The Error is occuring here try { if ($readfile = fopen ($file, "r")) { $curvalue = fread ($readfile, filesize($file)); $curvalue++; if (is_writable ($file)) { try { if ($writefile = fopen ($file, "w")) { fwrite ($writefile, $curvalue); echo "Wrote $curvalue to file."; } else { throw new exception ("Sorry, the file could not be opened"); } } catch (exception $e) { echo $e->getmessage(); } } else { echo "File could not be opened for writing"; } } else { throw new exception ("Sorry, the file could not be opened."); } } catch (exception $e) { echo $e->getmessage(); }}else{ echo "File does not exist.";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/#findComment-70772 Share on other sites More sharing options...
strawbshaker Posted August 7, 2006 Author Share Posted August 7, 2006 I must be doing something wrong.I have copied the code from wildteen88 as it is and saved it as a php file. I have then uploaded that file along with my blank testpage.txt on to my server. I am then trying to access the php page and I am still getting the same message.Am I going about testing this file all wrong? Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/#findComment-70784 Share on other sites More sharing options...
wildteen88 Posted August 7, 2006 Share Posted August 7, 2006 Does your host use PHP4? You can check the version of PHP your host has by creating a file called info.php and put this in it:[code]<?phpecho phpversion();?>[/code]Now upload that to your site and run it.What does it retrun. If its returns 5.x.x then you are running PHP5. If its PHP4.x.x or less then those version dont support try/catch. try/catch is for PHP5 and above Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/#findComment-70787 Share on other sites More sharing options...
strawbshaker Posted August 7, 2006 Author Share Posted August 7, 2006 Ahh! It all makes sense now! My server uses 4.4.2. I'll sort it out! Thanks for all you help! Quote Link to comment https://forums.phpfreaks.com/topic/16813-strange-error-can-any-one-help/#findComment-70791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.