Jump to content

Archived

This topic is now archived and is closed to further replies.

strawbshaker

Strange Error... Can any one help?

Recommended Posts

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 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.";
}
?>[/code]

Can anyone see the problem?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]<?php
echo 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

Share this post


Link to post
Share on other sites

×

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.