parse error, unexpected $end

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

parse error, unexpected $end

Post by 01chris »

I am trying to write a script which combines different pieces of code from various tutorials. (I know this probably isn't very good practice but I'm a newbie to PHP). When I run the script I get the error:

"Parse error: parse error, unexpected $end in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\upload\upload_file3.php on line 87"

Here is my script (please excuse the comment lines throughout the script - I copied & pasted directly from a tutorial):

Code: Select all

<?php
 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 99999999999))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
 
 
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
 
//This applies the function to our file
$ext = findexts ($_FILES['file']['name']) ; 
 
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $ran = rand () ;
 
    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";
 
    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "images/";
 
    //This combines the directory, the random file name, and the extension
    $target = $target . $ran2.$ext; 
 
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file has been uploaded as ".$ran2.$ext;
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
 
?>
 
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 99999999999))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("images/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "images/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
 
?>
 
 
Does anyone know why I get this error?

Thanks,
Chris
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: parse error, unexpected $end

Post by Zoxive »

You never close your first If statement.

Add a closing bracket "}" around line 14.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: parse error, unexpected $end

Post by JAM »

Around line 12, the 'else' looks misplaced aswell... Else what?
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

Re: parse error, unexpected $end

Post by 01chris »

Thanks for both of your prompt replies,

I tried adding an extra curley bracket & still had the same problem. I also removed the else and have the same problem.

Any other ideas?

Also, I have just realised there seems to be duplicate bits of code - I must have done something wrong somewhere because a new script starts on line 52, this should not be there. I have removed this extra code from my script but I just get the same error on a different line now.

It now looks like this

Code: Select all

<?php
 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 99999999999))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
else
{
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
}
//This applies the function to our file
$ext = findexts ($_FILES['file']['name']) ; 
 
    //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
    $ran = rand () ;
 
    //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
    $ran2 = $ran.".";
 
    //This assigns the subdirectory you want to save into... make sure it exists!
    $target = "images/";
 
    //This combines the directory, the random file name, and the extension
    $target = $target . $ran2.$ext; 
 
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file has been uploaded as ".$ran2.$ext;
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
 
?>
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

Re: parse error, unexpected $end

Post by 01chris »

I think I have sorted it!

Thank you very much for your suggestions but after playing around with the code a bit more I think its working properly. I guess I should have read it properly in the first place rather than copying and pasting - that taught me a lesson.

Anyway here is my (working) code:

Code: Select all

<?php
  
 if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/pjpeg"))
 && ($_FILES["file"]["size"] < 99999999999))
   {
   if ($_FILES["file"]["error"] > 0)
     {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
     }
   else
     {
  
  
  
  
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
     //This function separates the extension from the rest of the file name and returns it
 function findexts ($filename)
 {
 $filename = strtolower($filename) ;
 $exts = split("[/\\.]", $filename) ;
 $n = count($exts)-1;
 $exts = $exts[$n];
 return $exts;
 }
  
 //This applies the function to our file
 $ext = findexts ($_FILES['file']['name']) ;
  
     //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
     $ran = rand () ;
  
     //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
     $ran2 = $ran.".";
  
     //This assigns the subdirectory you want to save into... make sure it exists!
     $target = "images/";
  
     //This combines the directory, the random file name, and the extension
     $target = $target . $ran2.$ext;
  
 if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
 {
 echo "The file has been uploaded as ".$ran2.$ext;
 }
 else
 {
 echo "Sorry, there was a problem uploading your file.";
 }
      }
    }
  }
else
  {
  echo "Invalid file";
  }
  
?>
Thanks once again everyone

Chris
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: parse error, unexpected $end

Post by Zoxive »

If you are having simple problems like this, you really need to get a editor with highlighting, and start indenting/formatting consistently.

In your last post you still are not closing that if statement.

Code: Select all

<?php
 
function findexts ($filename){
  $filename = strtolower($filename) ;
  $exts = split("[/\\.]", $filename) ;
  $n = count($exts)-1;
  $exts = $exts[$n];
  return $exts;
}
 
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 99999999999)){
  if ($_FILES["file"]["error"] > 0){
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  }
  //This applies the function to our file
  $ext = findexts ($_FILES['file']['name']) ;
   
  //This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
  $ran = rand () ;
 
  //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
  $ran2 = $ran.".";
 
  //This assigns the subdirectory you want to save into... make sure it exists!
  $target = "images/";
 
  //This combines the directory, the random file name, and the extension
  $target = $target . $ran2.$ext;
   
  if(move_uploaded_file($_FILES['file']['tmp_name'], $target)){
    echo "The file has been uploaded as ".$ran2.$ext;
  }else{
    echo "Sorry, there was a problem uploading your file.";
  }
}else{
  echo 'IF Statment Was False';
}
 
?>
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

Re: parse error, unexpected $end

Post by 01chris »

Zoxive I think that might be my old code you have there??

I probably made my last post as you were writing yours.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: parse error, unexpected $end

Post by Zoxive »

01chris wrote:Zoxive I think that might be my old code you have there??

I probably made my last post as you were writing yours.
Yes, but I would still try and accomplish some of my points. It is very hard to look at your current code with brackets all over the place.

Look at the difference between yours and mine structure wise.

Nothing is perfect, but it just shows how a little formatting can go along way.
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

Re: parse error, unexpected $end

Post by 01chris »

Ok, thank you. Does your code do the same as mine just written in a different way?

I will try and tidy my code up a bit.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: parse error, unexpected $end

Post by Zoxive »

01chris wrote:Ok, thank you. Does your code do the same as mine just written in a different way?

I will try and tidy my code up a bit.
Its just a snippet from your 2nd post.

I was just using it as an example to show the difference of keeping formatting consistent.

It makes the code a lot easier to read on the fly.
01chris
Forum Newbie
Posts: 8
Joined: Mon Feb 04, 2008 7:30 am

Re: parse error, unexpected $end

Post by 01chris »

Oh right. OK.

Sorry for my 'newbieness'
Post Reply