note 56434 added to function.zip-open

From: Date: Fri, 02 Sep 2005 11:08:21 +0000
Subject: note 56434 added to function.zip-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-94656@lists.php.net to get a copy of this message
If your PHP installation does not have the zip_open function, and you can't install it for whatever reason, you can use these functions instead, if the server has access to the "unzip" utility (most Linux systems do). So far I have tested these only in Fedora Core 3. Use at your own risk. <?php function ShellFix($s) { return "'".str_replace("'", "'\''", $s)."'"; } function zip_open($s) { $fp = @fopen($s, 'rb'); if(!$fp) return false; $lines = Array(); $cmd = 'unzip -v '.shellfix($s); exec($cmd, $lines); $contents = Array(); $ok=false; foreach($lines as $line) { if($line[0]=='-') { $ok=!$ok; continue; } if(!$ok) continue; $length = (int)$line; $fn = trim(substr($line,58)); $contents[] = Array('name' => $fn, 'length' => $length); } return Array('fp' => $fp, 'name' => $s, 'contents' => $contents, 'pointer' => -1); } function zip_read(&$fp) { if(!$fp) return false; $next = $fp['pointer'] + 1; if($next >= count($fp['contents'])) return false; $fp['pointer'] = $next; return $fp['contents'][$next]; } function zip_entry_name(&$res) { if(!$res) return false; return $res['name']; } function zip_entry_filesize(&$res) { if(!$res) return false; return $res['length']; } function zip_entry_open(&$fp, &$res) { if(!$res) return false; $cmd = 'unzip -p '.shellfix($fp['name']).' '.shellfix($res['name']); $res['fp'] = popen($cmd, 'r'); return !!$res['fp']; } function zip_entry_read(&$res, $nbytes) { return fread($res['fp'], $nbytes); } function zip_entry_close(&$res) { fclose($res['fp']); unset($res['fp']); } function zip_close(&$fp) { fclose($fp['fp']); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.zip-open.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56434 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56434&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56434&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94656) next »