[solved] Upload | invalid image width/height/filename check?
Posted: Thu Aug 21, 2008 8:20 am
In my upload form, I can check whether an image is of the file types I allow or has the max allowed filesize, but I don't know how I can get the width/height of the image and if it is over 400x300 to disallow upload.
This is my code:
This is my code:
Code: Select all
<?
//load functions
require_once('inc/functions.php');
switch($_GET['file']){
case 'upload':
// UPLOAD FILE
//Maximum file size.
$MAX_SIZE = 1000000;
//valid extensions
$FILE_EXTS = array('.jpg','.png','.gif');
//Get server paths
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "files/uploads/images/";
$upload_url = $url_dir."/files/";
//get file name
$file_name = $_FILES['userfile']['name'];
$temp_name = $_FILES['userfile']['tmp_name'];
$file_size = $_FILES['userfile']['size'];
$file_path = $upload_dir.$file_name;
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
//replace illegal characters
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
//File Size Check
if( $file_size > $MAX_SIZE)
{
echo '<center>Error: The file size is over 1 Mb</center>';
}
else
{
//File type Check
if (!in_array($file_ext, $FILE_EXTS))
{
echo '<center>Error: Invalid File Type Specified, Allowed extensions are .jpg, .gif and .png</center>';
}
else
{
//connect to database
require_once('inc/database-connect.php');
//make upload entry
$db_insert="INSERT INTO images (image_id,image_src,image_alt)
VALUES
('i++','$file_name','image')";
if (!mysql_query($db_insert,$db_connection))
{
die("<font color='red'><img src='files/images/layout/cms-error.png' />Error</font>: " . mysql_error());
}
//fileupload
$result = move_uploaded_file($temp_name, $file_path);
if ($result=1)
{
echo "<center>$file_name was uploaded successfully!</center>";
}
else
{
echo "<center>Error: File could not be uploaded!</center>";
exit;
}
}
}
break;
default:
echo "<center>Select file to upload</center>";
}
?>
<html>
<head>
<title>FILE upload</title>
<link rel=stylesheet href=style.css>
</head>
<body>
<br>
<center>
<font color=red></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post" action="upload.php?file=upload">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>
</small>
</center>