Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    help me on the upload class !!

    Hello forums
    Now i am totally shifting towards OOP..
    I had tried to make a upload class but it didnt worked .here is the code

    uploader.class.php
    PHP Code:
    <?php
    class uploader
    {
        var 
    $uploadDir;
        var 
    $thumbDir;
        var 
    $newFileName;
        
    /*var $file_name;
        var $file_type;
        var $file_size;
        var $file_temp_dir;*/
        
    var $fileInfo = array();
        var 
    $maxFileSize;    
        var 
    $allowTypes = array();
        var 
    $errMsg "";
        var 
    $suMsg "";
        
    /***/
        
    var $fileExt;    
        
        function 
    getExtension()
        {
            
    $fileExt explode(".",$this->$fileInfo['name']);
            
    $this->fileExt $fileExt[1];
        }
        
        function 
    checkTypes()
        {
            if(!
    in_array($this->fileInfo['type'],$this->allowTypes))
            {
                return 
    FALSE;
            }
            else
                return 
    TRUE;
        }        
        
        
        function 
    doUpload()
        {
            
    // Check the Size
            /* If fileSize > maxSize */
            
    if($this->fileInfo['size'] > $this->maxFileSize)
            {
                
    $this->errMsg .= "File Size is larger<br />";
            }
            
            
    /* If fileSize == 0 */
            
    else if($this->fileInfo['size'] == 0)
            {
                
    $this->errMsg .= "No file uploaded <br />";
            }
            
            
    /* Check the Types */
            
    else if(!$this->checkTypes())
            {
                
    $this->errMsg .= "Invalid file type !! <br />";
            }
            
            
    /* If everything goes fine then Upload */
            
    else
            {            
                
    //upload to destDir
                
    echo "Final step";
                
    $uploadPath $this->uploadDir."/".$this->fileInfo['name'];
                
    move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
                
    //finally rename
                
    $ext $this->getExtension();
                
    $newUploadPath $this->uploadDir."/".$this->newFileName.".".ext;
                if(
    rename($uploadPath,$newUploadPath))
                {
                    
    $this->suMsg "Sucessfully Uploaded & Renamed !!";
                }
                
            }    
        }
        
        function 
    deleteFile()
        {
        }
        
        function 
    renameFile()
        {
        }
        
    }
    ?>
    action.php
    PHP Code:
    <?php
    if(isset($_POST[] ...)
    {
      include 
    "uploader.class.php";
    $uploaderObj = new uploader();
      
    //assign all the variable properties of uploader.class.php
    $uploaderObj->doUpload();
    }
    ?>
    But nothing happens
    What i want ?
    1>Any techniques for checking how the class is working ie any debugging tips
    2>Any changes in above code to make it efficient and effective Note: its for PHP4 ie any tips n modifications
    3> when i try to echo $uploaderObj->getExtension(), it gives the follwing error
    Fatal error: Cannot access empty property in C:\Program Files\xampp\htdocs\designtoko\libs\uploader.class.php on line 21
    and why it is so

    Thanks for reading my post.

    Thanks in advance to all of you and awaiting for the results...

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok.

    This:
    $this->$fileInfo['name'])
    Is not correct, it will attempt to find $fileInfo as a variable itself. Remember, $this now contains fileInfo as a property, and is accessed using $this->fileInfo. Any times you see it with the two dollar signs, and it is not using a local scope variable under that name, you must change it to the classes scope.

    Second.
    I don't see a constructor for your class. Offhand I cannot recall how picky php is when it requires a constructor, but normally you cannot create an instance of an object using new without the constructor. Since you did not recieve an error about access properties on a non-object, I would assume that php calls a default constructor. With that said, you could use the method in a static fasion: myClass::myFunc();

    And lastly, I do not see how any data is being passed between your object and your calls. I just don't see how the files are being passed to the class, or is there more information being used in the action.php that defines that? Remember as well, that $_POST is superglobal and as such, can be accessed within your class itself, you can create an error handler in there if you would like. I'm just not seeing where the files is being passed in...
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks all of You!!
    Finally i had made the uploader class...its working..
    PHP Code:
    <?php
    //uploader.class.php
    class uploader
    {    
        var 
    $uploadDir;    
        var 
    $newFileName;    
        var 
    $fileInfo = array();
        var 
    $maxFileSize;       
        var 
    $allowTypes = array();      
        var 
    $fileToDelete;
        var 
    $errMsg "";
        var 
    $upMsg "";    
        var 
    $delMsg "";
            
        function 
    getExtension()
        {
            
    $fileExt explode(".",$this->fileInfo['name']);
            return 
    $this->fileExt $fileExt[1];
        }
        
        function 
    checkTypes()
        {
            if(!
    in_array($this->fileInfo['type'],$this->allowTypes))
            {
                return 
    FALSE;
            }
            else
                return 
    TRUE;
        }              
        
        
        function 
    checkUpload()
        {
            
    // Check the Size
            /* If fileSize > maxSize */
            
    if($this->fileInfo['size'] > $this->maxFileSize)
            {
                
    //array_push($this->errMsg,"File Size is larger");
                
    $this->errMsg "File Size is larger <br />";
                return 
    FALSE;
            }
           
            
    /* If fileSize == 0 */
            
    if($this->fileInfo['size'] == 0)
            {
                
    //array_push($this->errMsg,"No file uploaded");
                
    $this->errMsg .= "No file uploaded <br />";
                return 
    FALSE;
            }
           
            
    /* Check the Types */
            
    else if(!$this->checkTypes())
            {
                
    //array_push($this->errMsg,"Invalid file type !!");
                
    $this->errMsg .= "Invalid file type !! <br />";
                return 
    FALSE;
            }
           
            
    /* If everything goes fine then Upload */
            
    else
            {                     
                return 
    TRUE;                       
            }              
                   
        }
        
        function 
    doUpload()
        {
            
    $uploadPath $this->uploadDir."/".$this->fileInfo['name'];
            
    move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
            
    //finally rename
            
    $ext $this->getExtension();
            
    $newUploadPath $this->uploadDir."/".$this->newFileName.".".$ext;
            if(@
    rename($uploadPath,$newUploadPath))
            {
                
    $this->upMsg "Sucessfully Uploaded & Renamed !!";
            }
            else
            {
                
    $this->upMsg "Unable to Rename !!";
            }
            
        }
        
        function 
    deleteFile()
        {
            if(
    file_exists($this->fileToDelete))
            {
                if(
    unlink($this->fileToDelete))
                {
                    
    $this->delMsg "File Successfully Deleted !!";    
                }
                else            
                {
                    
    $this->delMsg "Unable to Delete the file !!";
                }
            }
            else
            {
                
    $this->delMsg "Such file Doesnt exists !!";
            }
            return 
    $this->delMsg;
        }
           
    }
    ?>
    accessing part:
    PHP Code:
    <?php
    include "uploader.class.php";
    $uploaderObj = new uploader();
    $uploaderObj->uploadDir         "../uploads/images/";
            
    $uploaderObj->fileInfo          $_FILES['client_img'];        
            
    $uploaderObj->maxFileSize     1048576//in bytes
            
    $uploaderObj->allowTypes     = array("image/jpeg","image/jpg");        
            
            
    //Check Upload and Insert the Image Contents in DB
            
    if($uploaderObj->checkUpload())
            {
                
    $clientImagesObj->insert($clientID,$caption);
                
    $uploaderObj->newFileName  $clientImagesObj->id;
                
    //Finally Upload the file
                
    $uploaderObj->doUpload();
                 echo 
    $uploaderObj->upMsg;
            }
            else
            {
                echo 
    $uploaderObj->errMsg;
            }    
    ?>
    What i want?
    1> any modifications that make the code effective like one i shoulnt use explode to find the extension in case image1.name.jpg
    2> i am not getting the concatenated erros, is there any effective way of assigning and displaying erros
    3>any suggestions

    Thanks in advance to all of You !!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok I see, gots it working, now your interested in some cleanup right?
    I'll walk you through a couple of things I would recommend.
    First, variable assignments. Now, I do know that this is exclusively used on a oop php 4 engine, but I'd still make a couple simple modifications to save you time in the future.
    Write mutator methods for your properties. Anytime you see an $obj->property, you should write a getProperty() and setProperty() method for the class. Php Version 5 has an overloaded built in __get and __set, which easily simplifies this process. This will get you around the property scope (public private protected) without any concerns. Fortunatly, if you run a php4 class on a php5 engine, it will work as is, so this is really your choice.
    For getting the extension, this is an appropriate method to use. The only recommendation I would give you is to check for the last period in the given string, instead of the first. There are different concepts you can use for this, but with what you currently have you can obtain the index using: count(arrLength) - 1 instead. This sould give you the last value of the array. Another option would be to use the string family functions, such as str_split and whatnots, and even pattern matching.
    If you have it available, I would also use getimagesize() in conjunction with image_type_to_mime_type to compare what the image actually is. Just because the extension is .jpeg doesn't mean its not really a .php script.

    The final thing I would recommend is the placement of your upload directory. I would place it above your directory root to stop users from directly accessing the uploaded files. This may seem silly as you need to write a script to access them, however it will help to add that tiny extra buffer to protect your website integrety should someone upload a php file masked as an image.
    Hope that helps you out!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    One more Question?
    If i would like to generate thumbnail what should i do?
    1>make another class thumbNailer.class.php
    2>or use with uploader.class.php

    Suggest me to make the Thumbnail script for uploader.class.php
    Thanks in advance

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If the thumbnail is a separate stored image based off the uploader, I'd put it in the uploader class. If its a dynamically created thumbnail, I'd create a new class for it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have the following code
    PHP Code:
    <?php
    class uploader
    {    
        var 
    $uploadDir;    
        var 
    $newFileName;    
        var 
    $fileInfo = array();
        var 
    $maxFileSize;       
        var 
    $allowTypes = array();      
        var 
    $fileToDelete;
        
    //var $errMsg = "";
        
    var $errMsg = array();
        var 
    $upMsg "";    
        var 
    $delMsg "";
        var 
    $randNo "";    
            
        function 
    getExtension()
        {
            
    $fileExt substr(strrchr($this->fileInfo['name'],"."),1);
            
    //$fileExt = explode(".",$this->fileInfo['name']);
            
    return $this->fileExt $fileExt;
        }
        
        function 
    checkTypes()
        {
            if(!
    in_array($this->fileInfo['type'],$this->allowTypes))
            {
                return 
    FALSE;
            }
            else
                return 
    TRUE;
        }              
        
        
        function 
    checkUpload()
        {
            
    // Check the Size
            /* If fileSize > maxSize */
            
    if($this->fileInfo['size'] > $this->maxFileSize)
            {
                
    //array_push($this->errMsg,"File Size is larger");
                
    $this->errMsg[] = "File Size is larger <br />";
                return 
    FALSE;
            }
           
            
    /* If fileSize == 0 */
            
    else if($this->fileInfo['size'] == 0)
            {
                
    //array_push($this->errMsg,"No file uploaded");
                
    $this->errMsg[] = "No file uploaded <br />";
                return 
    FALSE;
            }
           
            
    /* Check the Types */
            
    else if(!$this->checkTypes())
            {
                
    //array_push($this->errMsg,"Invalid file type !!");
                
    $this->errMsg[] = "Invalid file type !! <br />";
                return 
    FALSE;
            }
           
            
    /* If everything goes fine then Upload */
            
    else
            {                     
                return 
    TRUE;                       
            }              
                   
        }
        
        function 
    doUpload()
        {
            
    $uploadPath $this->uploadDir."/".$this->fileInfo['name'];
            @
    move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
            
    //finally rename
            
    $ext $this->getExtension();
            
    $newUploadPath $this->uploadDir."/".$this->newFileName.".".$ext;
            if(@
    rename($uploadPath,$newUploadPath))
            {
                
    $this->upMsg "Sucessfully Uploaded & Renamed !!";
            }
            else
            {
                
    $this->upMsg "Unable to Rename !!";
            }
            
        }
        
        function 
    deleteFile()
        {
            if(
    file_exists($this->fileToDelete))
            {
                if(
    unlink($this->fileToDelete))
                {
                    
    $this->delMsg "File Successfully Deleted !!";    
                }
                else            
                {
                    
    $this->delMsg "Unable to Delete the file !!";
                }
            }
            else
            {
                
    $this->delMsg "Such file Doesnt exists !!";
            }
            return 
    $this->delMsg;
        }
        
        function 
    getErrors()
        {
            return 
    $this->errMsg;
        }
        
        function 
    genRandomNo()
        {
            
    //using uniqid() or microtime() for unique no generation
          
    $randNo substr(md5(uniqid(microtime())), 08);
          return 
    $randNo;
        }
           
    }
    ?>
    PHP Code:
    <?php
    //including and creating object goes here...
    $uploaderObj->uploadDir         "../uploads/images/";
            
    $uploaderObj->fileInfo          $_FILES['client_img'];        
            
    $uploaderObj->maxFileSize     1048576//in bytes
            
    $uploaderObj->allowTypes     = array("image/jpeg","image/jpg");        
            
            
    //Check Upload and Insert the Image Contents in DB
            
    if($uploaderObj->checkUpload())
            {
                
    $clientImagesObj->insert($clientID,$caption);
                
    $uploaderObj->newFileName  $clientImagesObj->id;
                
    //Finally Upload the file
                
    $uploaderObj->doUpload();
                 echo 
    $uploaderObj->upMsg;
            }
            else
            {
                
    print_r($uploaderObj->getErrors());
                
    //echo $uploaderObj->getErrors();            
            
    }        
    ?>
    Problem with the code:
    1> when i add .mp3 of very large size it gives only one error message but it should give two error messages ie it give error message "File is larger !!" but it should give "File is larger !!" & "Invalid file type !!". what is the problem with the code ?
    Help !!
    Thanks in advance

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You are using if/else with the returns. It finds the first error and returns a false boolean. To change it, you need to step through the possible error conditions setting the appropriate error messages, and return if the array according to its null value. False if it is not null, true if it is null.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Mr. Fou-Lu
    Can you help a bit with some codes Please ?
    Thanks again !!

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What kind of codes? If they are related to this class, post it in the thread. Otherwise, you can send it to me via pm and I'll take a look at it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    You are using if/else with the returns. It finds the first error and returns a false boolean. To change it, you need to step through the possible error conditions setting the appropriate error messages, and return if the array according to its null value. False if it is not null, true if it is null.
    codes related to errors and for the above case


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •