Voldemort Posted April 16, 2007 Share Posted April 16, 2007 I have a Badges directory on my site with some gifs and other things in it. I've gotten it to list only the badges, but I can't get the zip functions to work. I don't know where the code came from, my friend has been using it on a smaller scale with no trouble for a while, but I've been editing it for the past 45 minutes trying to get it to work. For one reason or another, the zip file comes up empty and spits out errors. <?php class zipfile { var $datasec = array(); var $ctrl_dir = array(); var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; var $old_offset = 0; function add_dir($name) { $name = str_replace("", "/", $name); $fr = "\x50\x4b\x03\x04"; $fr .= "\x0a\x00"; $fr .= "\x00\x00"; $fr .= "\x00\x00"; $fr .= "\x00\x00\x00\x00"; $fr .= pack("V",0); $fr .= pack("V",0); $fr .= pack("V",0); $fr .= pack("v", strlen($name) ); $fr .= pack("v", 0 ); $fr .= $name; $fr .= pack("V", 0); $fr .= pack("V", 0); $fr .= pack("V", 0); $this -> datasec[] = $fr; $new_offset = strlen(implode("", $this->datasec)); $cdrec = "\x50\x4b\x01\x02″"; $cdrec .="\x00\x00″"; $cdrec .="\x0a\x00″"; $cdrec .="\x00\x00″"; $cdrec .="\x00\x00″"; $cdrec .="\x00\x00\x00\x00″"; $cdrec .= pack("V",0); $cdrec .= pack("V",0); $cdrec .= pack("V",0); $cdrec .= pack("v", strlen($name) ); $cdrec .= pack("v", 0 ); $cdrec .= pack("v", 0 ); $cdrec .= pack("v", 0 ); $cdrec .= pack("v", 0 ); $ext = "\x00\x00\x10\x00″"; $ext = "\xff\xff\xff\xff"; $cdrec .= pack("V", 16 ); $cdrec .= pack("V", $this -> old_offset ); $cdrec .= $name; $this -> ctrl_dir[] = $cdrec; $this -> old_offset = $new_offset; return; } function add_file($data, $name) { $fp = fopen($data,"r"); $data = fread($fp,filesize($data)); fclose($fp); $name = str_replace("", "/", $name); $unc_len = strlen($data); $crc = crc32($data); $zdata = gzcompress($data); $zdata = substr ($zdata, 2, -4); $c_len = strlen($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", strlen($name) ); $fr .= pack("v", 0 ); $fr .= $name; $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($name) ); $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 .= $name; $this -> ctrl_dir[] = $cdrec; } function file() { $data = implode("", $this -> datasec); $ctrldir = implode("", $this -> ctrl_dir); return $data . $ctrldir . $this -> eof_ctrl_dir . pack("v", sizeof($this -> ctrl_dir)) . pack("v", sizeof($this -> ctrl_dir)) . pack("V", strlen($ctrldir)) . pack("V", strlen($data)) . "\x00\x00″"; } } $path = "Badges/" ; $exclude = array (0 => '.', 1 => '..', 2 => 'index.html', 3 => 'index.htm', 4 => 'index.php', 5 => '', 6 => 'marquee_gray.gif', 7 => 'scifirocket_violet.gif', 8 => 'woodenscreen_ultramarine.gif', 9 => 'roller_ultramarine.gif', 10 => 'rotator.php', 11 => 'scifirocket_violet.PNG', 12 => 'R7_gpillow_golden.png', 13 => 'show_media.php', 14 => '.htaccess', 15 => 'woodenscreen_ultramarine.PNG', 16 => 'rotator.php' ) ; if (!isset($file)) { // Open Directory and Gather Files Info $media = array() ; $handler = opendir($path) ; $i = 0 ; while ($temp = readdir($handler)) { $fileInfo = getFileInfo ($temp) ; if ($fileInfo['type']) { $media[$i] = $fileInfo ; $i++ ; } } closedir ($handler) ; unset ($fileInfo) ; } function getFileInfo ($file) { global $path, $exclude ; $result = array() ; $tempArray = array() ; $goodFile = true ; for ($i = 0; $i < count($exclude); $i++) { if ($file == $exclude[$i]) { $goodFile = false ; } } if ($goodFile && !is_dir($path.$file)) { $result['file'] = $file ; $result['date'] = filemtime ($path . $file) ; $tempArray = explode ('.', $file) ; if (count($tempArray) < 2) { $result['type'] = "" ; $result['name'] = $tempArray[0] ; } else { $result['type'] = strtolower($tempArray[count($tempArray) - 1]) ; $result['name'] = "" ; for ($j = 0; $j < count($tempArray) - 1; $j++) { $result['name'] .= $tempArray[$j] ; $result['name'] .= " " ; } $result['name'] = trim ($result['name']) ; $result['name'] = ereg_replace ('_', ' ', $result['name']) ; } unset ($tempArray) ; } else { $result['type'] = "" ; } return $result ; } $zipTest = new zipfile(); $zipTest->add_dir("Badges/"); foreach($media as $stuff => $stuff2){ $name = "Badges/" . $stuff2['file']; $zipTest->add_file($name, $name); } Header("Content-type: application/octet-stream"); Header ("Content-disposition: attachment; filename=zipTest.zip"); echo $zipTest->file(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/ Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 1. are you using php4 or php5? 2. what are the errors? 3. you know there's an OOP child-forum here. Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/#findComment-230017 Share on other sites More sharing options...
Voldemort Posted April 16, 2007 Author Share Posted April 16, 2007 1. PHP5. Tried PHP4 too, didn't make a difference. 2. The zip file contains 163 files named ";", and when I try to unzip it, it tells me there's no valid files in there. 3. I'm 99.pi% sure this isn't an OOP problem - it's the code inside the object Too bad this is the only zip creation code I can find... Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/#findComment-230026 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 so you're trying to zip up all the badges in a certain directory? ever heard of www.php.net/zip ? or google for that matter? Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/#findComment-230034 Share on other sites More sharing options...
Voldemort Posted April 16, 2007 Author Share Posted April 16, 2007 I googled for an hour today, couldn't find much of anything. I looked at PHP.net's zip functions but they look way too complicated for me to understand... I guess I'll give up on this for now, I've got other things I need to do tonight. Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/#findComment-230038 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 well, to be honest, your code looks more complicated than php's zip extension. there's no point in trying to beat it into submission when there's a php extension that does it already. Quote Link to comment https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/#findComment-230046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.