#native_company# #native_desc#
#native_cta#

File uploads made easy Page 4

By Darren Beale
on August 31, 2007

The main function in this script is for the validation of our upload, here we do things like check the file type, and if an image, what it’s dimensions are.

<?php

function validate_upload($the_file) {
 
global 
$my_max_file_size$image_max_width$image_max_height,$allowed_types,$the_file_type,$registered_types;
    
$start_error "n<b>Error:</b>n<ul>";
    
    if (
$the_file == "none") { # do we even have a file?
    
        
$error .= "n<li>You did not upload anything!</li>";
    
    } else { 
# check if we are allowed to upload this file_type
    
        
if (!in_array($the_file_type,$allowed_types)) {
            
$error .= "n<li>The file that you uploaded was of a ".
                
"type that is not allowed, you are only
                allowed to upload files of the type:n<ul>"
;
            while (
$type current($allowed_types)) {
                
$error .= "n<li>" $registered_types[$type] . " (" $type ")</li>";
                
next($allowed_types);
            }
            
$error .= "n</ul>";
        }
    
        if (
ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {
        
            
$size GetImageSize($the_file);
            list(
$foo,$width,$bar,$height) = explode(""",$size[3]);
    
            if (
$width $image_max_width) {
                
$error .= "n<li>Your image should be no wider than " 
                    
$image_max_width " Pixels</li>";
            }
            
            if (
$height $image_max_height) {    
                
$error .= "n<li>Your image should be no higher than " 
                    
$image_max_height " Pixels</li>";
            }
        
        }
        
        if (
$error) {
            
$error $start_error $error "n</ul>";
            return 
$error;
        } else {
            return 
false;
        }
    }
 } 
# END validate_upload

?>

Notes:

1-4: we start our function off and tell PHP what variables have a global scope (i.e. originated outside this function), this also saves us having to use $GLOBALS[] everywhere.
5: Start our error message off
6-9: if there is no file, $the_file is set to ‘none’ so we check if the user actually specified a file to upload
13-21: Here, our in_array function and the two arrays we built earlier come into play, we loop through the $allowed_types array and see if the PHP set $the_file_type is in there, if it is the script knows that it is allowed to accept this. If it is not allowed, then we use the $registered_types array to give a sensible error message.
23-35: If the uploaded file is an image, we want to check that it’s dimensions are smaller than our pre-defined maximum values. We use the PHP function getimagesize() to check the file dimensions and build up the error string if it is too big.
37-42: If we have anything in $error, we finish the string off and return it, else we return false. There that’s the hard work done.