Skip to content

Commit 16cb34c

Browse files
committed
Version 0.2.7 : Added \PhpOffice\Common\File::fileGetContents() (with support of zip://)
1 parent c9be70c commit 16cb34c

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@
4242
## 0.2.6
4343

4444
### Changes
45-
- `\PhpOffice\Common\Text::utf8ToUnicode()` became `public`.
45+
- `\PhpOffice\Common\Text::utf8ToUnicode()` became `public`.
46+
47+
## 0.2.7
48+
49+
### Features
50+
- Added `\PhpOffice\Common\File::fileGetContents()` (with support of zip://)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.6
1+
0.2.7

src/Common/File.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ public static function fileExists($pFilename)
5151
return file_exists($pFilename);
5252
}
5353
}
54+
/**
55+
* Returns the content of a file
56+
*
57+
* @param string $pFilename Filename
58+
* @return string
59+
*/
60+
public static function fileGetContents($pFilename)
61+
{
62+
if (!self::fileExists($pFilename)) {
63+
return false;
64+
}
65+
if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
66+
// Open ZIP file and verify if the file exists
67+
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
68+
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
69+
70+
$zip = new \ZipArchive();
71+
if ($zip->open($zipFile) === true) {
72+
$returnValue = $zip->getFromName($archiveFile);
73+
$zip->close();
74+
return $returnValue;
75+
}
76+
return false;
77+
}
78+
// Regular file contents
79+
return file_get_contents($pFilename);
80+
}
5481

5582
/**
5683
* Returns canonicalized absolute pathname, also for ZIP archives

tests/Common/Tests/FileTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ public function testFileExists()
3636
$this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml'));
3737
$this->assertFalse(File::fileExists('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml'));
3838
}
39+
/**
40+
*/
41+
public function testGetFileContents()
42+
{
43+
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR;
44+
$this->assertInternalType('string', File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo.png'));
45+
$this->assertFalse(File::fileGetContents($pathResources.'images'.DIRECTORY_SEPARATOR.'PHPPowerPointLogo_404.png'));
46+
$this->assertInternalType('string',File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#[Content_Types].xml'));
47+
$this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'Sample_01_Simple.pptx#404.xml'));
48+
$this->assertFalse(File::fileGetContents('zip://'.$pathResources.'files'.DIRECTORY_SEPARATOR.'404.pptx#404.xml'));
49+
}
3950

4051
/**
4152
*/

0 commit comments

Comments
 (0)