...

View Full Version : Call to a member function add_file() on a non-object



tomfmason
09-02-2006, 11:25 PM
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
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


function add_file($file, $dir) {
$name = str_replace("\\", "/", $dir);
$unc_len = strlen($file);
$crc = crc32($file);
$zdata = gzcompress($file);
$zdata = substr($zdata, 2, -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

Fumigator
09-03-2006, 12:23 AM
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.

Lee Stevens
09-03-2006, 12:49 AM
This


$zip_file -> add_file($file, $zip_dir);//this is line 21


Should be:


$zipfile -> add_file($file, $zip_dir);//this is line 21



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum