TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-30-2008, 10:32 PM   #1 (permalink)
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
Old 01-30-2008, 10:37 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Functions and OOP are for reusability, you really aren't reusing that code. Inline code would be best for something that small.
__________________

Village Idiot is offline  
Reply With Quote
Old 01-31-2008, 08:33 PM   #3 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

You could make your HTML code CSS valid (Xbsr) and so on, but I don't see why you're using an class for something that simple. No offense tho.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:59 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design