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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Call to a member function add_file() on a non-object

    This maybe a rather simple one to fix but I am not seeing were I am going wrong. I am getting the following error Fatal error: Call to a member function add_file() on a non-object in zip.php on line 21

    The zip.php is rather simple. Basicly what it does it goes through a loop and searches the current directory and then adds the sub directorys and all files to a zip file.

    Here is the zip.php

    PHP Code:
    <?php
    require('includes/class.php');
    clearstatcache();
    $zip_dir ="./"$_SERVER['HTTP_HOST'] ."." date("Ymd") . ".zip";
    if (
    is_dir($zip_dir) == false) {
        
    mkdir($zip_dir);
    }
    $folders = array();
    $zipfile = new zipFile();
    $zipfile -> add_dir($zip_dir);
    $path "./";
    if (
    $dirc opendir($path)) {
        
    $d 0;
        while ((
    $file readdir($dirc)) !== false) {
           if (
    $file != '.' && $file != '..') {
               if (
    is_dir($path $file)) {
                   
    $d++;
                   
    $folders[$d] = $path $file;
                   
    $zipfile -> add_dir($zip_dir "/" $file);
               }else{
                  
    $zip_file -> add_file($file$zip_dir);//this is line 21
               
    }
           }
       }
    }
    closedir($dir);
    function 
    get_sub_folders($folders) {
       
    $d count($folders);
       foreach (
    $folders as $folder) {
          if (
    $dirc opendir($folder)) {
              while (
    $file readdir($dirc)) {
                 
    $pathto $folder "/" $file;
                 if (
    $file != '.' && $file != '..' && !in_array($folders)) {
                     if (
    is_dir($file) == true) {
                         
    $d++;
                         
    $folders[$d] = $pathto;
                         
    $zipfile -> add_dir($zip_dir "/" $pathto);
                     }else{
                         
    $zipfile -> add_file($file$zip_dir "/" $folder);
                     }
                     
    $folders get_sub_folders($folders);
                 }
             }
          }
          
    closedir($folders);
       }
       
    sort($folders);
       return 
    $folders;
    }
    $getfolders get_sub_folders($folders);
    $zfile zfile();
    ?>
    Here is the add_file function
    PHP Code:
        function add_file($file$dir) {
            
    $name str_replace("\\""/"$dir);
            
    $unc_len strlen($file);
            
    $crc crc32($file);
            
    $zdata gzcompress($file);
            
    $zdata substr($zdata2, -4);
            
    $c_len = ($zdata);
            
            
    $fr "\x50\x4b\x03\x04";
            
    $fr .= "\x14\x00";
            
    $fr .= "\x00\x00";
            
    $fr .= "\x08\x00";
            
    $fr .= "\x00\x00\x00\x00";
            
    $fr .= pack("V"$crc);
            
    $fr .= pack("V"$c_len);
            
    $fr .= pack("V"$unc_len);
            
    $fr .= pack("v"$dir);
            
    $fr .= pack("v"0);
            
    $fr .= $dir;
            
    $fr .= $zdata;
            
    $fr .= pack("V"$crc);
            
    $fr .= pack("V"$c_len);
            
    $fr .= pack("V"$unc_len);
            
            
    $this -> datasec[] = $fr;
            
            
    $new_offset strlen(implode(""$this->datasec));
            
            
    $cdrec "\x50\x4b\x01\x02";
            
    $cdrec .= "\x00\x00";
            
    $cdrec .= "\x14\x00";
            
    $cdrec .= "\x00\x00";
            
    $cdrec .= "\x08\x00";
            
    $cdrec .= "\x00\x00\x00\x00";
            
    $cdrec .= pack("V"$crc);
            
    $cdrec .= pack("V"$c_len);
            
    $cdrec .= pack("V"$unc_len);
            
    $cdrec .= pack("v"strlen($dir));
            
    $cdrec .= pack("v"0);
            
    $cdrec .= pack("v"0);
            
    $cdrec .= pack("v"0);     
            
    $cdrec .= pack("v"0);
            
    $cdrec .= pack("V"32);
            
    $cdrec .= pack("V"$this-> old_offset);
            
            
    $this-> old_offset $new_offset;
            
            
    $cdrec .= $dir;
            
    $this -> cltr_dir[] = $cdrec;
        } 
    Any suggestions as to what I am doing wrong?

    Thanks,
    Tom

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is line 21 the call to add_file() inside get_sub_folders()? If it is, then you're getting this error because the function get_sub_folders() doesn't know anything about the object $zip_file, instantiated outside the function's scope.

    If that's not the right line... then I dunno.

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Location
    UK, London, Dartford
    Posts
    221
    Thanks
    3
    Thanked 14 Times in 14 Posts
    This
    PHP Code:
    $zip_file -> add_file($file$zip_dir);//this is line 21 
    Should be:
    PHP Code:
    $zipfile -> add_file($file$zip_dir);//this is line 21 


  •  

    Posting Permissions

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