Jump to content

Recommended Posts

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&#8243;";
$cdrec .="\x00\x00&#8243;";
$cdrec .="\x0a\x00&#8243;";
$cdrec .="\x00\x00&#8243;";
$cdrec .="\x00\x00&#8243;";
$cdrec .="\x00\x00\x00\x00&#8243;";
$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&#8243;";
$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&#8243;";
$fr .= "\x14\x00&#8243;";
$fr .= "\x00\x00&#8243;";
$fr .= "\x08\x00&#8243;";
$fr .= "\x00\x00\x00\x00&#8243;";
$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&#8243;";
$cdrec .="\x00\x00&#8243;";
$cdrec .="\x14\x00&#8243;";
$cdrec .="\x00\x00&#8243;";
$cdrec .="\x08\x00&#8243;";
$cdrec .="\x00\x00\x00\x00&#8243;";
$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&#8243;";
}
}

$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();
?>

Link to comment
https://forums.phpfreaks.com/topic/47168-solved-dont-know-what-to-title-this/
Share on other sites

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... -_-

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.