+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: PHP: Upload Class

  1. #1
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,843
    Blog Entries
    25

    PHP: Upload Class

    I've been making an OOP library of all methods/functions I use on a daily basis that need validation. Hope someone else can make use if it.

    Upload.php
    Code:
    <?php
    /**
     * This class allows a user to upload and 
     * validate their files.
     *
     * @author John Ciacia <Sidewinder@extreme-hq.com>
     * @version 1.0
     * @copyright Copyright (c) 2007, John Ciacia
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     */
    class Upload {
        
        
    /**
         *@var string contains the name of the file to be uploaded.
         */
        
    var $FileName;
        
    /**
         *@var string contains the temporary name of the file to be uploaded.
         */
        
    var $TempFileName;
        
    /**
         *@var string contains directory where the files should be uploaded.
         */
        
    var $UploadDirectory;
        
    /**
         *@var string contains an array of valid extensions which are allowed to be uploaded.
         */
        
    var $ValidExtensions;
        
    /**
         *@var string contains a message which can be used for debugging.
         */
        
    var $Message;
        
    /**
         *@var integer contains maximum size of fiels to be uploaded in bytes.
         */
        
    var $MaximumFileSize;
        
    /**
         *@var bool contains whether or not the files being uploaded are images.
         */
        
    var $IsImage;
        
    /**
         *@var string contains the email address of the recipient of upload logs.
         */
        
    var $Email;
        
    /**
         *@var integer contains maximum width of images to be uploaded.
         */
        
    var $MaximumWidth;
        
    /**
         *@var integer contains maximum height of images to be uploaded.
         */
        
    var $MaximumHeight;

        function 
    Upload()
        {

        }

        
    /**
         *@method bool ValidateExtension() returns whether the extension of file to be uploaded
         *    is allowable or not.
         *@return true the extension is valid.
         *@return false the extension is invalid.
         */
        
    function ValidateExtension()
        {

            
    $FileName trim($this->FileName);
            
    $FileParts pathinfo($FileName);
            
    $Extension strtolower($FileParts['extension']);
            
    $ValidExtensions $this->ValidExtensions;

            if (!
    $FileName) {
                
    $this->SetMessage("ERROR: File name is empty.");
                return 
    false;
            }

            if (!
    $ValidExtensions) {
                
    $this->SetMessage("WARNING: All extensions are valid.");
                return 
    true;
            }

            if (
    in_array($Extension$ValidExtensions)) {
                
    $this->SetMessage("MESSAGE: The extension '$Extension' appears to be valid.");
                return 
    true;
            } else {
                
    $this->SetMessage("Error: The extension '$Extension' is invalid.");
                return 
    false;  
            }

        }

        
    /**
         *@method bool ValidateSize() returns whether the file size is acceptable.
         *@return true the size is smaller than the alloted value.
         *@return false the size is larger than the alloted value.
         */
        
    function ValidateSize()
        {
            
    $MaximumFileSize $this->MaximumFileSize;
            
    $TempFileName $this->GetTempName();
            
    $TempFileSize filesize($TempFileName);

            if(
    $MaximumFileSize == "") {
                
    $this->SetMessage("WARNING: There is no size restriction.");
                return 
    true;
            }

            if (
    $MaximumFileSize <= $TempFileSize) {
                
    $this->SetMessage("ERROR: The file is too big. It must be less than $MaximumFileSize and it is $TempFileSize.");
                return 
    false;
            }

            
    $this->SetMessage("Message: The file size is less than the MaximumFileSize.");
            return 
    true;
        }

        
    /**
         *@method bool ValidateExistance() determins whether the file already exists. If so, rename $FileName.
         *@return true can never be returned as all file names must be unique.
         *@return false the file name does not exist.
         */
        
    function ValidateExistance()
        {
            
    $FileName $this->FileName;
            
    $UploadDirectory $this->UploadDirectory;
            
    $File $UploadDirectory $FileName;

            if (
    file_exists($File)) {
                
    $this->SetMessage("Message: The file '$FileName' already exist.");
                
    $UniqueName rand() . $FileName;
                
    $this->SetFileName($UniqueName);
                
    $this->ValidateExistance();
            } else {
                
    $this->SetMessage("Message: The file name '$FileName' does not exist.");
                return 
    false;
            }
        }

        
    /**
         *@method bool ValidateDirectory()
         *@return true the UploadDirectory exists, writable, and has a traling slash.
         *@return false the directory was never set, does not exist, or is not writable.
         */
        
    function ValidateDirectory()
        {
            
    $UploadDirectory $this->UploadDirectory;

            if (!
    $UploadDirectory) {
                
    $this->SetMessage("ERROR: The directory variable is empty.");
                return 
    false;
            }

            if (!
    is_dir($UploadDirectory)) {
                
    $this->SetMessage("ERROR: The directory '$UploadDirectory' does not exist.");
                return 
    false;
            }

            if (!
    is_writable($UploadDirectory)) {
                
    $this->SetMessage("ERROR: The directory '$UploadDirectory' does not writable.");
                return 
    false;
            }

            if (
    substr($UploadDirectory, -1) != "/") {
                
    $this->SetMessage("ERROR: The traling slash does not exist.");
                
    $NewDirectory $UploadDirectory "/";
                
    $this->SetUploadDirectory($NewDirectory);
                
    $this->ValidateDirectory();
            } else {
                
    $this->SetMessage("MESSAGE: The traling slash exist.");
                return 
    true;
            }
        }

        
    /**
         *@method bool ValidateImage()
         *@return true the image is smaller than the alloted dimensions.
         *@return false the width and/or height is larger then the alloted dimensions.
         */
        
    function ValidateImage() {
            
    $MaximumWidth $this->MaximumWidth;
            
    $MaximumHeight $this->MaximumHeight;
            
    $TempFileName $this->TempFileName;

        if(
    $Size = @getimagesize($TempFileName)) {
            
    $Width $Size[0];   //$Width is the width in pixels of the image uploaded to the server.
            
    $Height $Size[1];  //$Height is the height in pixels of the image uploaded to the server.
        
    }

            if (
    $Width $MaximumWidth) {
                
    $this->SetMessage("The width of the image [$Width] exceeds the maximum amount [$MaximumWidth].");
                return 
    false;
            }

            if (
    $Height $MaximumHeight) {
                
    $this->SetMessage("The height of the image [$Height] exceeds the maximum amount [$MaximumHeight].");
                return 
    false;
            }

            
    $this->SetMessage("The image height [$Height] and width [$Width] are within their limitations.");     
            return 
    true;
        }

        
    /**
         *@method bool SendMail() sends an email log to the administrator
         *@return true the email was sent.
         *@return false never.
         *@todo create a more information-friendly log.
         */
        
    function SendMail() {
            
    $To $this->Email;
            
    $Subject "File Uploaded";
            
    $From "From: Uploader";
            
    $Message "A file has been uploaded.";
            
    mail($To$Subject$Message$From);
            return 
    true;
        }


        
    /**
         *@method bool UploadFile() uploads the file to the server after passing all the validations.
         *@return true the file was uploaded.
         *@return false the upload failed.
         */
        
    function UploadFile()
        {

            if (!
    $this->ValidateExtension()) {
                die(
    $this->GetMessage());
            } 

            else if (!
    $this->ValidateSize()) {
                die(
    $this->GetMessage());
            }

            else if (
    $this->ValidateExistance()) {
                die(
    $this->GetMessage());
            }

            else if (!
    $this->ValidateDirectory()) {
                die(
    $this->GetMessage());
            }

            else if (
    $this->IsImage && !$this->ValidateImage()) {
                die(
    $this->GetMessage());
            }

            else {

                
    $FileName $this->FileName;
                
    $TempFileName $this->TempFileName;
                
    $UploadDirectory $this->UploadDirectory;

                if (
    is_uploaded_file($TempFileName)) { 
                    
    move_uploaded_file($TempFileName$UploadDirectory $FileName);
                    return 
    true;
                } else {
                    return 
    false;
                }

            }

        }

        
    #Accessors and Mutators beyond this point.
        #Siginificant documentation is not needed.
        
    function SetFileName($argv)
        {
            
    $this->FileName $argv;
        }

        function 
    SetUploadDirectory($argv)
        {
            
    $this->UploadDirectory $argv;
        }

        function 
    SetTempName($argv)
        {
            
    $this->TempFileName $argv;
        }

        function 
    SetValidExtensions($argv)
        {
            
    $this->ValidExtensions $argv;
        }

        function 
    SetMessage($argv)
        {
            
    $this->Message $argv;
        }

        function 
    SetMaximumFileSize($argv)
        {
            
    $this->MaximumFileSize $argv;
        }

        function 
    SetEmail($argv)
        {
            
    $this->Email $argv;
        }
       
        function 
    SetIsImage($argv)
        {
            
    $this->IsImage $argv;
        }

        function 
    SetMaximumWidth($argv)
        {
            
    $this->MaximumWidth $argv;
        }

        function 
    SetMaximumHeight($argv)
        {
            
    $this->MaximumHeight $argv;
        }   
        function 
    GetFileName()
        {
            return 
    $this->FileName;
        }

        function 
    GetUploadDirectory()
        {
            return 
    $this->UploadDirectory;
        }

        function 
    GetTempName()
        {
            return 
    $this->TempFileName;
        }

        function 
    GetValidExtensions()
        {
            return 
    $this->ValidExtensions;
        }

        function 
    GetMessage()
        {
            if (!isset(
    $this->Message)) {
                
    $this->SetMessage("No Message");
            }

            return 
    $this->Message;
        }

        function 
    GetMaximumFileSize()
        {
            return 
    $this->MaximumFileSize;
        }

        function 
    GetEmail()
        {
            return 
    $this->Email;
        }

        function 
    GetIsImage()
        {
            return 
    $this->IsImage;
        }

        function 
    GetMaximumWidth()
        {
            return 
    $this->MaximumWidth;
        }

        function 
    GetMaximumHeight()
        {
            return 
    $this->MaximumHeight;
        }
    }


    ?>
    Usage:
    index.php
    Code:
    <?php
    include("Upload.php");

    echo 
    "<form enctype='multipart/form-data' action='index.php' method='POST'>"
    ."<input name='upload' type='file' /><input type='submit' value='Upload' />"
    ."</form>";


    if(
    $_FILES['upload']['tmp_name']) {
        
    $upload = new Upload();
        
    $upload->SetFileName($_FILES['upload']['name']);
        
    $upload->SetTempName($_FILES['upload']['tmp_name']);
        
    $upload->SetUploadDirectory("/var/www/upload/"); //Upload directory, this should be writable
        
    $upload->SetValidExtensions(array('gif''jpg''jpeg''png')); //Extensions that are allowed if none are set all extensions will be allowed.
        //$upload->SetEmail("Sidewinder@codecall.net"); //If this is set, an email will be sent each time a file is uploaded.
        //$upload->SetIsImage(true); //If this is set to be true, you can make use of the MaximumWidth and MaximumHeight functions.
        //$upload->SetMaximumWidth(60); // Maximum width of images
        //$upload->SetMaximumHeight(400); //Maximum height of images
        
    $upload->SetMaximumFileSize(300000); //Maximum file size in bytes, if this is not set, the value in your php.ini file will be the maximum value
        
    echo $upload->UploadFile();

    }

    ?>
    Last edited by John; 12-04-2007 at 12:34 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97
    Very nicely done Sidewinder. I'll defiantly be using this in the future.

  3. #3
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,220
    Blog Entries
    6

    Re: PHP: Upload Class

    Thanks. I just needed this for my website!

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,218
    Blog Entries
    13

    Re: PHP: Upload Class

    Your surname is Ciacia? Cool.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,843
    Blog Entries
    25

    Re: PHP: Upload Class

    Quote Originally Posted by TcM View Post
    Thanks. I just needed this for my website!
    The image validation's are not 100% secure.

    Quote Originally Posted by Xav View Post
    Your surname is Ciacia? Cool.
    Yes.

  6. #6
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,220
    Blog Entries
    6

    Re: PHP: Upload Class

    I don't need that. I just need the file extension validation and the file size. I tested the extension validation, seems to be working fine, I didn't test the size validation though.

    I had to edit it a little bit so it works with my script, but so far it's perfect.

    BTW I am using it to upload more than one file (from 1 to 10 files) depending on the user. Now my problem is this, This class upload one file to temp, validates it, and them moves it to the folder. Now as I am uploading more than one file, is there a way to make it, upload all the files to temp, then validates them all, and them moves them all at the same moment. So if one file is invalid, the user will have to upload all the files again, because in this way I will have duplicate files.

    Thanks.
    Last edited by TcM; 05-17-2008 at 07:18 AM.

  7. #7
    Programming God DevilsCharm is an unknown quantity at this point DevilsCharm's Avatar
    Join Date
    Jul 2006
    Posts
    885

    Re: PHP: Upload Class

    Isn't it great when people use your features!

  8. #8
    Programmer ReekenX is an unknown quantity at this point ReekenX's Avatar
    Join Date
    Jan 2007
    Posts
    134

    Re: PHP: Upload Class

    Well, great tutorial. Maybe one day I will make Upload library in my PHP framework. Someday

  9. #9
    Code Warrior Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W is a name known to all Brandon W's Avatar
    Join Date
    Sep 2008
    Location
    Australia
    Age
    17
    Posts
    4,839
    Blog Entries
    10

    Re: PHP: Upload Class

    Yet again, this is another old thread. But it doesn't matter, I really like this class I just need to keep reading it over and over and over. After that I should now how everything works fine, then I can write my own upload class

    I would +rep but used it on your other tutorial

    Thanks!
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  10. #10
    Newbie loopb is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    1

    Unhappy Re: PHP: Upload Class

    Great but i still need to change the name of the upload file to:
    date('YMD')+time();

    bu it does not upload the file with the extention, How do I do that?
    Please help, I don't have idea ):.

    loopb

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 06:55 AM
  2. PHP Introduction
    By clookid in forum PHP Tutorials
    Replies: 10
    Last Post: 01-16-2007, 05:17 AM