Jump to content

Strange Error... Can any one help?


strawbshaker

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