SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    at first
    move_uploaded_file($this->fileInfo['tmp_name'],$uploadPath);
    this command must take the new file name not a folder ,
    why u upload then rename the file ?

    note: you can add function that create the uploadPath directory before upload
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where have you assigned the variable $fileInfo for your uploaded files? Is there any error displaying? If yes what exactly it says? Try out passing your uploaded file to the class constructor like this:
    PHP Code:
    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 
    uploader($upfiles){
            
    $this->$fileInfo;
        }
        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(){
        }

    And use like this:
    PHP Code:
    if(isset($_POST[] ...){
        include 
    "uploader.class.php";
        
    $uploaderObj = new uploader($_FILES['uploadedfile']);
        
    //assign all the variable properties of uploader.class.php
        
    $uploaderObj->doUpload();

    And don't use such explode function to get extension of the uploaded file. Find the type of the file and get extension. If the file already named with some dot (.) like abc.name.jpeg, then what will be the extension of the file?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 !!

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1.
    PHP Code:
    $fileext preg_split("/[.]/"$origfile);
    $fileext array_reverse($fileext); 
    That way you will get the file extension if the file is named bla.bla.jpeg.

  6. #6
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mr.pcw
    one more questions?
    is there any effective way of assigning and displaying erros

  7. #7
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can return true on success, and error string on fail.
    PHP Code:
     $result $uploader->upload();
    if(
    $result!==true){
        
    $view->error_page("File upload failed!");


  8. #8
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

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