Page 1 of 1

restricting dimensions of a image on upload

Posted: Sat Mar 06, 2010 8:18 pm
by fael097
hi. i have an image upload code that works just great, with file type and size restrictions, but im having a hard time to make dimension restriction work.
using

Code: Select all

list($width, $height)=getimagesize($_FILES['userfile']);
will return a weird error:
Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in C:\Program Files\xampp\htdocs\upload.php on line 28
and it will upload big files anyway. of course theres no such file, since im uploading the file to a database and not a directory.

what to do?
heres my whole code:

Code: Select all

 
<?php
if (isset($_COOKIE["usern"]))
  echo "<p>Logged in as ".$_COOKIE["usern"].". | <a href='logout.php'>Logout</a></p>";
else
  header("location: login.php");
?>
 
<html>
<head>
<title>Upload</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="shortcut icon" href="images/favicon.ico" />
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</form>
 
<?php
include("variables.php");
 
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{   
    $image=$_FILES['userfile'];
    $maxwidth=151;
    $maxheight=181;
    list($width, $height)=getimagesize("$image");
    
    if((($_FILES["userfile"]["type"]=="image/gif") || 
        ($_FILES["userfile"]["type"]=="image/jpeg") || 
        ($_FILES["userfile"]["type"]=="image/png") || 
        ($_FILES["userfile"]["type"]=="image/pjpeg")) && 
        ($_FILES["userfile"]["size"]<204800) &&
        (($width<$maxwidth) || ($height<$maxheight)))
    {
 
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
 
if(!get_magic_quotes_gpc())
    {
    $fileName=addslashes($fileName);
    }
 
$query="INSERT INTO upload (name, size, type, content, user ) VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$username')";
 
if($count!=0) 
    {
    $query="UPDATE upload SET name='$fileName', size='$fileSize', type='$fileType', content='$content'  WHERE user='$username'";
    }
mysql_query($query)or die(mysql_error());
 
echo "File $fileName uploaded.<br />";
    }
    else
    {
    echo "Invalid file.";
    }
}
 
echo "Your current avatar: <br />";
echo $avatar;
 
?>
 
</body>
</html>
 

Re: restricting dimensions of a image on upload

Posted: Sat Mar 06, 2010 10:37 pm
by infolock
It's because you are trying to get the dimensions of an array, instead of the actual filename.

So, $_FILES['userfile'] is not your filename, it's just the associative keyname for your identifying $_FILES array.

If you want to get the imagesize of the file, you'll have to supply the tmp_name to your key. so your code would instead be:

Code: Select all

 
$image = $_FILES['userfile'];
list($width, $height) = getimagesize($image['tmp_name']);
// Or, you could just do this list($width, $height) = getimagesize($_FILES['userfile']['tmp_name']);
 
Also, I don't konw why you are doing getimagesize("$image"); with the variable being enclosed with double quotes as the double quotes aren't needed unless you are trying to append some typed string information in with it.