View Single Post
Old 01-30-2008, 10:32 PM   #1 (permalink)
Gareth
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default How to improve my Upload Function?

Hey,

I finally delved into functions (maybe OOP?) to produce a really simple image upload system. I have the following, which works, but is there anywhere I can improve on? Id est syntax and expressions but NOT features, please? :)






INDEX.PHP

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <meta name="author" content="Gareth Price">

    <title>Upload It!</title>
    
    <style>
    
        body {
            width: 500px;
            margin: 0 auto;
            font:11px Tahoma, Arial, Helvetica, sans-serif;
            color:#666666;
        }
        
    </style>
</head>

<body>

    <h1>Upload It!</h1>

    <?php 
    
        
require('uploadit.class.php');                 // require class
            
        
$upload = new sui;                        // set variable
        
        
echo $upload->uploadform("uploads/");        // initiate upload form
    
?>

</body>
</html>
UPLOADIT.CLASS.PHP

PHP Code:
<?php

    
class sui{                                                // initiates class called uploader

        
public $path "http://localhost/uploadit/uploads/";        // the FULL path to the upload directory, must include trailing slash

    //********************************************************************************
**********************************************//

    
function uploadfile($filename$filesize$filewidth$fileheight$fileext$tmpname$dir$max_height$max_width$max_size){

        
$allowed = array("jpg","png","gif");                        // allowed file types
        // SET OTHER CONFIGS @ ~line 80

        
if(!in_array($fileext,$allowed)){                            // if the file extension in the array is NOT allowed ...
                     
echo "File type not supported: $fileext";        // echo out error message and display the extension which is not allowed
                
}
                 
             else{    

                if(
$filewidth>$max_width){                            // is the file too wide?
                    
echo "Image width is over the limit";            // echo out error message
                
}
                
            elseif(
$fileheight>$max_height){                        // is the file too high?
                    
echo "Image height is over the limit";            // echo out error message
                
}
                
                else{                                                
                
            if(
$filesize>$max_size){                                // is the file too big?
                    
echo "Image size is over the limit";            // echo out error message
                
}
                
            else{                                                    
// no errors to display.... move onto displaying links
                    
            
$rand rand(1000,100000);                                // we are now creating a random name
            
$newfilename urlencode($filename);                    
            
$newname $rand."_".$newfilename;                        // we add a random number, place an underscore, and add the original file name (you can change this)
            
$bb $this->path.$rand."_".$newfilename;                // setting $bb to the path AND the new file name put together.
            
            
$html "<img src=$bb alt=\"YourSite\" />";                // display the html for below
                                        
                
if(copy($tmpname$dir.$newname)){                    // if the copying of the image to the directory is succesful...
                                                                    // display the following (you can add more links here, eg thumbnails?)...
                    
echo "                                            
                        <p><a href=\"
$bb\"><img src=\"$bb\" border=\"0\" width=\"100\" height=\"100\" /></a></p>
                        
                        <p><strong>Direct Link</strong> <br />
                        <input type=\"text\" size=\"70\" value=\"
$bb\" /></p>
                        
                        <p><strong>BBCode</strong><br />
                        <input type=\"text\" size=\"70\" value=\"[img]"
.$bb."[/img]\" /></p>
                        
                        <p><strong>HTML</strong><br />
                        <input type=text size=70 value='
$html' /></p>                            
                        
                        <a href=\"index.php\">Upload again!</a>
                        
                        "
;}
    
                    else{                                            
// if the copying of image is UNsuccesful...
                        
echo "The copy was not successful";            // echo out error message
                        
}
                    }
                    }
                }                                                    
    }                                                                
// closing open brackets and function.
                
    //********************************************************************************
**********************************************//

    
function uploadform($directory){                                // initiates upload form
        
if(isset($_POST['upload'])){                                // has the user pressed submit? MUST equal the name in form below
            
            
if(!is_dir($directory)){                                // does the directory exist?
                
echo "Sorry, $directory was not found on the server.";
            }

        else{
            
$img_max_height "10000";                                // configurable settings follow... (in pixels and bytes)
            
$img_max_width "10000";
            
$img_max_size "800000";
            
$image_name addslashes($_FILES['file']['name']);
            
$image_size $_FILES['file']['size'];
            
$image_dimensions getimagesize($_FILES['file']['tmp_name']);
            
$image_width $image_dimensions[0];
            
$image_height $image_dimensions[1];
            
$image_extention explode(".",$image_name);
            
$extention strtolower($image_extention[count($image_extention)-1]);
            
$image_tmp_name $_FILES['file']['tmp_name'];

        
$this->uploadfile($image_name,$image_size,$image_width,$image_height,$extention$image_tmp_name$directory$img_max_height$img_max_width$img_max_size);
            }                                                         
// using the first function, uploadfile, we set the different configs and try to upload the file.
        
}
        
        else{                                                        
// if the user has NOT pressed submit show the form for them to do so!
            
return "
    <form enctype=\"multipart/form-data\" method=\"post\" action=\"
$_SERVER[PHP_SELF]\">
        <input type=\"file\" class=\"file\" name=\"file\" />
        <input type=\"submit\" name=\"upload\" value=\"Upload\" id=\"submit\" />
    </form>"
;
            }
        
        }                                                            
// closing open brackets and function.

    //********************************************************************************
**********************************************//

    
}                                                                // closing class.
    
?>
Thanks for any help :)

Gareth
Gareth is offline  
Reply With Quote