Click here to Skip to main content
14,699,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
	if(isset($_FILES["photo"]["error"])){
		if($_FILES["photo"]["error"]>0){
			echo "Error:".$_FILES["photo"]["error"]."<br>";
		}else{
			$allowed=array("jpg"=>"image/jpg","jpeg"=>"image/gif","png"=>"image/png");
			$filename=$_FILES["photo"]["name"];
			$filetype=$_FILES["photo"]["type"];
			$filesize=$_FILES["photo"]["size"];
			
			$ext=pathinfo($filename, PATHINFO_EXTENSION);
			if(!array_key_exists($ext,$allowed)) die("Error: Please select a valid file format.");
			
			$maxsize=5*1024*1024;
			if($filesize>$maxsize) die("Error: File size is larger than the allowed limit");
			
			
			if(in_array($filetype,$allowed)){
				if(file_exists("upload/".$_FILES["photo"]["name"])){
					echo $_FILES["photo"]["name"]."is already exists.";
				}else{
					move_uploaded_file($_FILES["photo"]["tmp_name"],"upload/".$_FILES["photo"]["name"]);
					echo "your file was uploaded successfully";
				} else{
            echo "Error: There was a problem uploading your file - please try again."; 
        }
    }
} else{
    echo "Error: Invalid parameters - please contact your server administrator.";
}
?>


What I have tried:

i have tried nothing and i would like to know the meaning of the very first IF and the second IF
Posted
Updated 24-Apr-17 10:16am
v3
Comments
ZurdoDev 24-Apr-17 13:25pm
   
You need to fix the syntax. You're missing a closing bracket or a semi-colon or something. Fix that first.

1 solution

You have two ELSE statements on the same IF. Match up the curly brackets.
if(in_array($filetype,$allowed)){
    if(file_exists("upload/".$_FILES["photo"]["name"])){
        echo $_FILES["photo"]["name"]."is already exists.";
    }else{
        move_uploaded_file($_FILES["photo"]["tmp_name"],"upload/".$_FILES["photo"]["name"]);
        echo "your file was uploaded successfully";
    } else{
        echo "Error: There was a problem uploading your file - please try again.";
    }

Should be:
if(in_array($filetype,$allowed)){
    if(file_exists("upload/".$_FILES["photo"]["name"])){
        echo $_FILES["photo"]["name"]."is already exists.";
    } else {
        move_uploaded_file($_FILES["photo"]["tmp_name"],"upload/".$_FILES["photo"]["name"]);
        echo "your file was uploaded successfully";
    }
} else {
    echo "Error: There was a problem uploading your file - please try again.";
}
   
Comments
harshal12345 25-Apr-17 12:36pm
   
Dave i tried what u said but its not working still the same error
Dave Kreskowiak 25-Apr-17 12:42pm
   
OK, I'm not going to give you a copy and paste answer. YOU have to carefully examine the code and make sure every opening and closing curly brace is paired up properly.

To make things easier to read, make sure a curly brace is NOT on the same line as code. It should be on it's own line all by itself. Everything should also be indented properly:

This:
if (some expression) {
 ... code ...
} else {
 ... code ...
}


becomes:
if (some expression)
{
    ... code ...
}
else
{
    ... code ...
}
harshal12345 25-Apr-17 13:21pm
   
OKAY i understand what are you saying i too like everything indented properly but when you copy and paste a code from other website you just want to get a output which i am not getting.and i already did what u said ...not helping
Dave Kreskowiak 25-Apr-17 13:23pm
   
If you're getting the exact same error, you've got the exact same problem. You have mismatched curly braces and the compiler is seeing a second else statement for a single if.

If you copied this code from somewhere, it's got a bug in it. There is a missing curly brace before the second to last else statement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900