Skip to content

Commit c759658

Browse files
committed
0.2.0
commit e340380 Author: Stan Hutcheon <[email protected]> Date: Tue Nov 1 16:56:42 2016 +0000 remove file output option commit 174b720 Author: Thomas Pfister <[email protected]> Date: Tue Nov 1 15:41:13 2016 +0100 * Fix temp filename-generation * Fix PHP Warnings commit 7e19ffd Author: Stan Hutcheon <[email protected]> Date: Tue Nov 1 16:14:43 2016 +0000 update codebase Merged #5
1 parent 2fea1e5 commit c759658

File tree

5 files changed

+113
-162
lines changed

5 files changed

+113
-162
lines changed

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,49 @@ Based on [planetbeing/partial-zip](https://github.com/planetbeing/partial-zip).
66

77
#### Usage:
88

9-
```composer require stnvh/php-partialzip ~0.1```
9+
```composer require stnvh/php-partialzip 0.2.x```
1010

1111
##### Method usage:
1212

1313
###### __construct($url, $file = false):
1414
Class init method
1515
```php
16-
<?php
17-
$p = new Partial('http://some.site.com/cats.zip', 'cat.png');
18-
# or
1916
$p = new Partial('http://some.site.com/cats.zip');
2017
```
2118

2219
###### index():
2320
Returns a list of all the files in the remote directory
2421
```php
25-
<?php
2622
/*...*/
27-
$list = implode(' ', $p->index()); # = 'cat.png cat2.png cat3.png'
23+
24+
$list = $p->index(); # = ('cat.png', 'cat2.png', 'cat3.png')
2825
```
2926

3027
###### find($fileName = false):
3128
Returns a parsed file object for use when fetching the remote file
3229
```php
33-
<?php
3430
/*...*/
35-
$file = $p->find(); # Returns the cat.png file object (as set on init)
36-
# or
37-
$file = $p->find('cat2.png'); # Search and return other file objects
38-
39-
# You can call methods here to fetch ZIP header information too
40-
# The full list of file header properties can be found in CDFile.php
41-
$size = $file->size(); # size in bytes
42-
$fullName = $file->name(); # full file name in zip, including path
31+
32+
# Search and return other file objects
33+
if($file = $p->find('cat2.png')) {
34+
# You can call methods here to fetch ZIP header information too
35+
# The full list of file header properties can be found in CDFile.php
36+
$size = $file->size(); # size in bytes
37+
$fullName = $file->name(); # full file name in zip, including path
38+
}
39+
4340
```
4441

45-
###### get($file, $output = false):
42+
###### get($file):
4643
Returns, or outputs the file fetched from the remote ZIP.
4744

48-
**Note**: You should ensure no content is outputted before running ```->get($file, true)``` as this will cause the file download to contain invalid data.
49-
*Hint*: put ```ob_start()``` at the start of your script, then run ```ob_clean()``` before calling get.
45+
**Note**: You should ensure no content is outputted before echo-ing ```->get()``` as this will cause the file download to contain invalid data.
46+
*Hint*: put ```ob_start()``` at the start of your script, then run ```ob_clean()``` before output.
5047
```php
51-
<?php
5248
/*...*/
53-
if($file) {
54-
$data = $p->get($file); # Return
55-
# or
56-
$p->get($file, true); # Output (download)
49+
50+
if($file = $p->find('cat3.png')) {
51+
$fileData = $p->get($file);
5752
}
5853
```
5954

@@ -68,13 +63,17 @@ use Stnvh\Partial\Zip as Partial;
6863

6964
ob_start(); # will capture all output
7065

71-
$p = new Partial('http://some.site.com/cats.zip', 'cat.png');
66+
$p = new Partial('http://some.site.com/cats.zip');
7267

7368
# Get file object
74-
$file = $p->find();
75-
if($file) {
76-
ob_clean(); # removes everything from current output to ensure file downloads correctly
77-
# Output to browser:
78-
$p->get($file, true);
69+
if($file = $p->find('cat.png')) {
70+
# removes everything from current output to ensure file downloads correctly
71+
ob_clean();
72+
73+
# Set appropriate headers and output to browser:
74+
header(sprintf('Content-Disposition: attachment; filename="%s"', $file->filename));
75+
header(sprintf('Content-Length: %d', $file->size));
76+
77+
echo $p->get($file);
7978
}
8079
```

src/Stnvh/Partial/Data/EOCD.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class EOCD extends PartialData {
1515
'CDEntries' => array(2, 'v*'),
1616
'CDSize' => array(4, 'V*'),
1717
'CDOffset' => array(4, 'V*'),
18-
'lenComment' => array(4, 'V*'),
18+
'lenComment' => array(2, 'V*'),
1919
'comment' => array('lenComment', false)
2020
);
2121
}

src/Stnvh/Partial/Data/PartialData.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PartialData {
2222
* @return void
2323
*/
2424
public function __construct($raw = false, $map = false) {
25-
$this->tempName = sys_get_temp_dir() . uniqid('CDFile~');
25+
$this->tempName = tempnam(sys_get_temp_dir(), 'CDFile~');
2626
if($raw) {
2727
$this->format($raw, $map);
2828
}
@@ -114,19 +114,19 @@ public function format($raw, $map = false) {
114114
$sect = substr($raw, (isset($pos[2]) ? $pos[2] : $i), $pos[0]);
115115
if($pos[1]) {
116116
$sect = unpack($pos[1], $sect);
117-
$sect = $sect[1];
117+
$sect = isset($sect[1]) ? $sect[1] : null;
118118
}
119119
$this->$name = $sect;
120120

121121
$i += $pos[0];
122122
}
123123

124-
if(!$this->lenHeader) {
124+
if(!isset($this->lenHeader)) {
125125
$this->lenHeader = $i;
126126
}
127127

128128
# If size not populated, fetch from 'extra field'
129-
if($this->method == 0x0008 && $this->extra && !$this->size) {
129+
if(isset($this->method) && $this->method == 0x0008 && $this->extra && !$this->size) {
130130
$_map = array(
131131
'crc32' => $map['crc32'],
132132
'compressedSize' => $map['compressedSize'],

src/Stnvh/Partial/Data/ZipInfo.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* @package Stnvh\Partial\Data
88
*/
99
class ZipInfo {
10-
public $centralDirectory = '';
11-
public $centralDirectoryEnd = '';
12-
public $localHeader = '';
10+
public $centralDirectory = array();
1311
public $length = 0;
1412
}

0 commit comments

Comments
 (0)