Restrict Image File Upload by Resolution (dpi)
Posted: Fri Feb 20, 2009 10:02 am
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
I have a form for users to upload image files and they get inserted into mysql database. I currently have restrictions on file types to only allow .jpg, I also have restrictions on image dimensions and file size. I would also like to restrict files from being uploaded that are below 300dpi. How can I achieve this? Current "upload.php" that gets action from the form works perfect, I just want to add a check for dpi of file into it. I realize of course I'll have to add a new field to my mysql Table, create the variable, and gather its data value from the image file. Code below:
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
I have a form for users to upload image files and they get inserted into mysql database. I currently have restrictions on file types to only allow .jpg, I also have restrictions on image dimensions and file size. I would also like to restrict files from being uploaded that are below 300dpi. How can I achieve this? Current "upload.php" that gets action from the form works perfect, I just want to add a check for dpi of file into it. I realize of course I'll have to add a new field to my mysql Table, create the variable, and gather its data value from the image file. Code below:
Code: Select all
<?php
# Check if a file has been uploaded
if(isset($_FILES['uploaded_file']))
{
# Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0)
{
# Connect to the database
$dbLink = mysqli_connect("hostname", "username", "password", "dbname");
if(mysqli_connect_errno())
{
die("MySQL connection failed: ". mysqli_connect_error());
}
# Define Variables and Gather all required data
$limit_size = 2097152;
$max_width = "600";
$max_height = "400";
$legal_type = "image/jpeg";
$legal_typeIE = "image/pjpeg";
$esize = "File size exceeds 2MB. Adjust and try to upload file again please.";
$etype = "File type must be .JPG format. Adjust and try to upload file again please.";
$edim = "File dimensions must equal 600x400 pixels. Adjust and try to upload file again please.";
$edpi = "File resolution must equal 300dpi. Adjust and try to upload file again please.";
$esucc = "File uploaded successfully. Thanks for contributing to the project.";
$eukn = "An unknow error occurred while uploading the file. Please try again.";
$name = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
$mime = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
$size = $_FILES['uploaded_file']['size'];
$data = mysqli_real_escape_string($dbLink, file_get_contents($_FILES ['uploaded_file']['tmp_name']));
# Check File Type
if(($mime != $legal_type) && ($mime != $legal_typeIE))
{
$passit = $etype;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
# Check File Dimensions
list($width, $height) = getimagesize($_FILES['uploaded_file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
$passit = $edim;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
# Check File Size
if ($size >= $limit_size)
{
$passit = $esize;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
else
{
# Create the SQL query
$query = "
INSERT INTO FileStorage (
FileName, FileMime, FileSize, FileData, Created
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
# Execute the query
$result = mysqli_query($dbLink, $query);
}
# Check if it was successfull
if($result)
{
$passit = $esucc;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
# Close the mysql connection
mysqli_close($dbLink);
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
exit();
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.