Giving an error

Discussion in 'PHP' started by Katy, Jan 25, 2008.

Thread Status:
Not open for further replies.
  1. #1
    Hello all! Could someone please, help me to insert an error into this code, so that if someone uploads an image that is bigger than 300x220 pixels it throws an error saying "The image you're trying to upload is too big. The allowed size is 300x220 pixels."

    Here's the code:

    
    //check if file type is valid.
                                    if( move_uploaded_file($_FILES['tf']['tmp_name'],$folder . $fname) )
                                    {
                                        if(move_uploaded_file($_FILES['ts']['tmp_name'],$folder2 . $fname2))
                                        {
                                            //if file is able to be uploaded upload it to the correct folder
                                            echo "Thank you for uploading your theme! You can now <a href='http://themes.rock-kitty.net/upload.php'>upload another theme</a> or return to the <a href='http://themes.rock-kitty.net'>homepage</a>";
                                            //send an email also
                                            $this->Email($name,$fname);
                                        }
                                        else
                                        {
                                            echo "Error uploading image.";
                                        }
                                    }
                                    else
                                    {
                                        echo "Error uploading zip/rar file";
                                    }
                                }
                                else
                                {
                                    //throw SQL problem telling there was a problem inputting data into database.
                                    echo "There was an error when trying to insert data into the database. Please contact the administrator.";
                                }
                        }
                        else
                        {
                            echo "This file type is not allowed.";
                        }
                        
                    }
                    else
                    {
                        echo "There was a field left empty, please, go <a href='javascript:history.go(-1)'>back</a> and check all fields.";
                    }
                
                }
            
            }
            function valid_file_type($file)
            {
            
                $file_types = array("zip","rar");
                $file = explode(".",$file);
                $f = $file[count($file)-1];
                if( in_array($f,$file_types) )
                {
                    return true;//return true if file type isn't .zip or .rar
                }
                return false;//return false if file type isn't .zip or .rar
            
            }
    
    PHP:
    Thanks! :)
     
    Katy, Jan 25, 2008 IP
  2. redvok

    redvok Active Member

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    //check if file type is valid.
                                    if( move_uploaded_file($_FILES['tf']['tmp_name'],$folder . $fname) )
                                    {
                                        if(move_uploaded_file($_FILES['ts']['tmp_name'],$folder2 . $fname2))
                                        {
    list($width, $height, $type, $attr) = getimagesize($folder2 . $fname2);
    if($width < 300 && $height<220){
                                            //if file is able to be uploaded upload it to the correct folder
                                            echo "Thank you for uploading your theme! You can now <a href='http://themes.rock-kitty.net/upload.php'>upload another theme</a> or return to the <a href='http://themes.rock-kitty.net'>homepage</a>";
                                            //send an email also
                                            $this->Email($name,$fname);
    }else{
    echo "The image you're trying to upload is too big. The allowed size is 300x220 pixels.";
    }
                                        }
                                        else
                                        {
                                            echo "Error uploading image.";
                                        }
                                    }
                                    else
                                    {
                                        echo "Error uploading zip/rar file";
                                    }
                                }
                                else
                                {
                                    //throw SQL problem telling there was a problem inputting data into database.
                                    echo "There was an error when trying to insert data into the database. Please contact the administrator.";
                                }
                        }
                        else
                        {
                            echo "This file type is not allowed.";
                        }
    
    PHP:
     
    redvok, Jan 25, 2008 IP
  3. Katy

    Katy Moderator Staff

    Messages:
    3,490
    Likes Received:
    513
    Best Answers:
    7
    Trophy Points:
    355
    #3
    Thanks for the reply.

    It gives an error, but still uploads the image..
     
    Katy, Jan 25, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Are the uploaded files actually compressed files (zip, rar)? Because you'd have to uncompress them first before checking the image size. The ZIPs won't be much of a problem, but the RARs might be, because you need the RAR Library to be installed on your server. (shell access might be sufficient as well)

    And which version of PHP are you using?

    Also, make sure to convert the file extension to lower case when comparing to the allowed ones. Because some users might have an unexpected case and this would cause the script to reject valid files.

    
    $f = strtolower($file[count($file)-1]);
    
    PHP:
     
    nico_swd, Jan 25, 2008 IP
  5. redvok

    redvok Active Member

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #5
    
    //check if file type is valid.
    list($width, $height, $type, $attr) = getimagesize($_FILES['tf']['tmp_name']);
    if($width < 300 && $height<220){
                                    if( move_uploaded_file($_FILES['tf']['tmp_name'],$folder . $fname) )
                                    {
                                        if(move_uploaded_file($_FILES['ts']['tmp_name'],$folder2 . $fname2))
                                        {
    
    
                                            //if file is able to be uploaded upload it to the correct folder
                                            echo "Thank you for uploading your theme! You can now <a href='http://themes.rock-kitty.net/upload.php'>upload another theme</a> or return to the <a href='http://themes.rock-kitty.net'>homepage</a>";
                                            //send an email also
                                            $this->Email($name,$fname);
    
                                        }
                                        else
                                        {
                                            echo "Error uploading image.";
                                        }
                                    }
                                    else
                                    {
                                        echo "Error uploading zip/rar file";
                                    }
                                }
                                else
                                {
                                    //throw SQL problem telling there was a problem inputting data into database.
                                    echo "There was an error when trying to insert data into the database. Please contact the administrator.";
                                }
                        }
                        else
                        {
                            echo "This file type is not allowed.";
                        }
    else{
    echo "The image you're trying to upload is too big. The allowed size is 300x220 pixels.";
    }
    
    PHP:
     
    redvok, Jan 25, 2008 IP
    Katy likes this.
  6. Katy

    Katy Moderator Staff

    Messages:
    3,490
    Likes Received:
    513
    Best Answers:
    7
    Trophy Points:
    355
    #6
    It gives me an error:

    Parse error: syntax error, unexpected T_ELSE in /home/***/public_html/***/***/***.php on line 134

    Line 134 is this:

    else{
     
    Katy, Jan 27, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Try adding a curly bracket right in front of the else{:
    
    } else {
    
    PHP:
     
    nico_swd, Jan 27, 2008 IP
  8. Katy

    Katy Moderator Staff

    Messages:
    3,490
    Likes Received:
    513
    Best Answers:
    7
    Trophy Points:
    355
    #8
    I tried, but it gave me:

    Parse error: syntax error, unexpected $end in /home/***/public_html/***/***/***.php on line 241

    :eek:
     
    Katy, Jan 27, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Lol, it'd be a little easier if we could see the rest of your code because there are a lot of "else"s which we don't know about. Anyway, give this a try. I don't see any apparent reasons why this wouldn't work.

    
    		//check if file type is valid.
    		list($width, $height) = @getimagesize($_FILES['ts']['tmp_name']);
    		
    		if ($width < 300 && $height < 220)
    		{
    			if (@move_uploaded_file($_FILES['tf']['tmp_name'], $folder . $fname) )
    			{
    				if (@move_uploaded_file($_FILES['ts']['tmp_name'], $folder2 . $fname2))
    				{
    					//if file is able to be uploaded upload it to the correct folder
    					echo "Thank you for uploading your theme! You can now <a href='http://themes.rock-kitty.net/upload.php'>upload another theme</a> or return to the <a href='http://themes.rock-kitty.net'>homepage</a>";
    					//send an email also
    					$this->Email($name,$fname);	
    				}
    				else
    				{
    					echo "Error uploading image.";
    				}
    			}
    			else
    			{
    				echo "Error uploading zip/rar file";
    			}
    		}
    		else
    		{
    			echo "The image you're trying to upload is too big. The allowed size is 300x220 pixels.";
    		}
    	}
    	else
    	{
    		//throw SQL problem telling there was a problem inputting data into database.
    		echo "There was an error when trying to insert data into the database. Please contact the administrator.";
    	}
    }
    else
    {
    	echo "This file type is not allowed.";
    }
    
    PHP:

    The last error you got means that there's a curly bracket missing. So if you get the same error again, perhaps you can spot it without having to post your full code.
     
    nico_swd, Jan 27, 2008 IP
  10. Katy

    Katy Moderator Staff

    Messages:
    3,490
    Likes Received:
    513
    Best Answers:
    7
    Trophy Points:
    355
    #10
    Sorry for the late reply, I've been very busy and didn't have to work on this issue.

    The code works, but it still uploads the theme even though it gives an error that the image size is too big.

    EDIT:

    Nico fixed it for me, thanks!
     
    Katy, Feb 17, 2008 IP
Thread Status:
Not open for further replies.