Jump to content


Photo

Strange Error... Can any one help?


  • Please log in to reply
5 replies to this topic

#1 strawbshaker

strawbshaker
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 August 2006 - 04:51 PM

Hi there,

I'm using the following bit of code and get the error message:

Parse error: parse error, unexpected '{' in /xxx/file.php on line 4

<?php
$file = "testpage.txt";
if (file_exists ($file)){
try { // The Error is occuring here
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.";
}
?>

Can anyone see the problem?

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 07 August 2006 - 05:01 PM

I can even run it with my own text file. So: no, don't see the problem.
RTFM is an almost extinct art form, it should be subsidized.

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 August 2006 - 05:05 PM

edit I made a complete tit of myself.

Going through your code and indenting it I found your  { and } brakets where unbalanced. Try this:
<?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.";
}

?>


#4 strawbshaker

strawbshaker
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 August 2006 - 05:52 PM

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?

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 August 2006 - 06:02 PM

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:
<?php
echo phpversion();
?>
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

#6 strawbshaker

strawbshaker
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 07 August 2006 - 06:08 PM

Ahh! It all makes sense now! My server uses 4.4.2. I'll sort it out! Thanks for all you help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users