getFileExtAttr() public static method

Get the file permissions for a file or directory, for use in the extFileAttr parameters.
Author: A. Grandt (php@grandt.com)
public static getFileExtAttr ( string $filename ) : string | boolean
$filename string
return string | boolean external ref field, or false if the file is not found.
 /**
  * Add the content to a directory.
  *
  * @author Adam Schmalhofer <*****@*****.**>
  * @author A. Grandt <*****@*****.**>
  *
  * @param string $realPath       Path on the file system.
  * @param string $zipPath        File path and name to be used in the archive.
  * @param bool   $recursive      Add content recursively, default is true.
  * @param bool   $followSymlinks Follow and add symbolic links, if they are accessible, default is true.
  * @param array  &$addedFiles     Reference to the added files, this is used to prevent duplicates, default is an empty array.
  *                               If you start the function by parsing an array, the array will be populated with the $realPath
  *                               and $zipPath kay/value pairs added to the archive by the function.
  * @param bool   $overrideFilePermissions Force the use of the file/dir permissions set in the $extDirAttr
  *                               and $extFileAttr parameters.
  * @param int    $extDirAttr     Permissions for directories.
  * @param int    $extFileAttr    Permissions for files.
  */
 public function addDirectoryContent($realPath, $zipPath, $recursive = true, $followSymlinks = true, &$addedFiles = array(), $overrideFilePermissions = false, $extDirAttr = self::EXT_FILE_ATTR_DIR, $extFileAttr = self::EXT_FILE_ATTR_FILE)
 {
     if (file_exists($realPath) && !isset($addedFiles[realpath($realPath)])) {
         if (is_dir($realPath)) {
             $this->addDirectory($zipPath, 0, null, $overrideFilePermissions ? $extDirAttr : ZipUtils::getFileExtAttr($realPath));
         }
         $addedFiles[realpath($realPath)] = $zipPath;
         $iter = new \DirectoryIterator($realPath);
         foreach ($iter as $file) {
             /* @var $file \DirectoryIterator */
             if ($file->isDot()) {
                 continue;
             }
             $newRealPath = $file->getPathname();
             $newZipPath = ZipUtils::pathJoin($zipPath, $file->getFilename());
             if (file_exists($newRealPath) && ($followSymlinks || !is_link($newRealPath))) {
                 if ($file->isFile()) {
                     $addedFiles[realpath($newRealPath)] = $newZipPath;
                     $this->addLargeFile($newRealPath, $newZipPath, 0, null, $overrideFilePermissions ? $extFileAttr : ZipUtils::getFileExtAttr($newRealPath));
                 } else {
                     if ($recursive) {
                         $this->addDirectoryContent($newRealPath, $newZipPath, $recursive, $followSymlinks, $addedFiles, $overrideFilePermissions, $extDirAttr, $extFileAttr);
                     } else {
                         $this->addDirectory($zipPath, 0, null, $overrideFilePermissions ? $extDirAttr : ZipUtils::getFileExtAttr($newRealPath));
                     }
                 }
             }
         }
     }
 }
示例#2
0
	public function test1(){

		error_reporting(E_ALL | E_STRICT);
		ini_set('error_reporting', E_ALL | E_STRICT);
		ini_set('display_errors', 1);
		//
		//// Example. Zip all .html files in the current directory and save to current directory.
		// Make a copy, also to the current dir, for good measure.
		//$mem = ini_get('memory_limit');
		//$extime = ini_get('max_execution_time');
		//
		////ini_set('memory_limit', '512M');
		ini_set('max_execution_time', 600);

		//include_once("ZipStream.php");
		//print_r(ini_get_all());

		//$fileTime = date("D, d M Y H:i:s T");

		$chapter1 = "Chapter 1\n"
			. "Lorem ipsum\n"
			. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec magna lorem, mattis sit amet porta vitae, consectetur ut eros. Nullam id mattis lacus. In eget neque magna, congue imperdiet nulla. Aenean erat lacus, imperdiet a adipiscing non, dignissim eget felis. Nulla facilisi. Vivamus sit amet lorem eget mauris dictum pharetra. In mauris nulla, placerat a accumsan ac, mollis sit amet ligula. Donec eget facilisis dui. Cras elit quam, imperdiet at malesuada vitae, luctus id orci. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque eu libero in leo ultrices tristique. Etiam quis ornare massa. Donec in velit leo. Sed eu ante tortor.\n";

		//$zip = new ZipStream("ZipStreamExample1.zip");
		$zip = new ZipStream('ZipStreamExample1.zip');

		$zip->setComment("Example Zip file for Large file sets.\nCreated on " . date('l jS \of F Y h:i:s A'));
		$zip->addFile("Hello World!\r\n", "Hello.txt");

		$zip->openStream("big one3.txt");
		$zip->addStreamData($chapter1."\n\n\n");
		$zip->addStreamData($chapter1."\n\n\n");
		$zip->addStreamData($chapter1."\n\n\n");
		$zip->closeStream();

		// For this test you need to create a large text file called "big one1.txt"
		if (file_exists("big one1.txt")) {
			$zip->addLargeFile("big one1.txt", "big one2a.txt", 0, null, ZipUtils::getFileExtAttr("big one1.txt"));

			$fhandle = fopen("big one1.txt", "rb");
			$zip->addLargeFile($fhandle, "big one2b.txt");
			fclose($fhandle);
		}

		$zip->addDirectory("Empty Dir");

		//Dir test, using the stream option on $zip->addLargeFile
		$fileDir = './';
		@$handle = opendir($fileDir);
		if ($handle) {
			while (false !== ($file = readdir($handle))) {
				if (($file != '.') && ($file != '..') && is_file($file)) {
					$zip->addLargeFile(($fileDir . $file), "dirTest/".$file, filectime($fileDir . $file));
				}
			}
		}

		// Add a directory, first recursively, then the same directory, but without recursion.
		// Naturally this requires you to change the path to ../test to point to a directory of your own.
		$zip->addDirectory("recursiveDir/");
		$zip->addDirectoryContent("../../test", "recursiveDir/test");
		$zip->addDirectoryContent("../../test", "recursiveDir/testFlat", FALSE);
		/*
		$addedFiles = array();
		$zip->addDirectoryContent("../test", "recursiveDir/testPermisssions", TRUE, TRUE, $addedFiles,
					TRUE, ZipStream::generateExtAttr(4, 4, 0, FALSE), ZipStream::generateExtAttr(4, 4, 0, TRUE));
		*/
		$zip->finalize(); // Mandatory, needed to send the Zip files central directory structure.
	}
示例#3
0
		. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec magna lorem, mattis sit amet porta vitae, consectetur ut eros. Nullam id mattis lacus. In eget neque magna, congue imperdiet nulla. Aenean erat lacus, imperdiet a adipiscing non, dignissim eget felis. Nulla facilisi. Vivamus sit amet lorem eget mauris dictum pharetra. In mauris nulla, placerat a accumsan ac, mollis sit amet ligula. Donec eget facilisis dui. Cras elit quam, imperdiet at malesuada vitae, luctus id orci. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque eu libero in leo ultrices tristique. Etiam quis ornare massa. Donec in velit leo. Sed eu ante tortor.\n";

$zip = new \PHPZip\Zip\Stream\ZipStream('ZipStreamExample1.zip'); // $zip = new ZipStream("ZipStreamExample1.zip");

$zip->setComment("Example Zip file for Large file sets.\nCreated on " . date('l jS \of F Y h:i:s A'));
$zip->addFile("Hello World!\r\n", "Hello.txt");

$zip->openStream("big one3.txt");
$zip->addStreamData($chapter1."\n\n\n");
$zip->addStreamData($chapter1."\n\n\n");
$zip->addStreamData($chapter1."\n\n\n");
$zip->closeStream();

// For this test you need to create a large text file called "big one1.txt"
if (file_exists("big one1.txt")) {
	$zip->addLargeFile("big one1.txt", "big one2a.txt", 0, null, \PHPZip\Zip\Core\ZipUtils::getFileExtAttr("big one1.txt"));

	$fhandle = fopen("big one1.txt", "rb");
	$zip->addLargeFile($fhandle, "big one2b.txt");
	fclose($fhandle);
}

$zip->addDirectory("Empty Dir");

//Dir test, using the stream option on $zip->addLargeFile
$fileDir = './';
@$handle = opendir($fileDir);
if ($handle) {
	/* This is the correct way to loop over the directory. */
	while (false !== ($file = readdir($handle))) {
		if (($file != '.') && ($file != '..') && is_file($file)) {
示例#4
0
	private static function _createAndSendZipArchive(ZipArchiveFile $zip, $fileDir = './'){

		// Archive comments don't really support utf-8. Some tools detect and read it though.
		$zip->setComment("Example Zip file.\nАрхив Комментарий\nCreated on " . date('l jS \of F Y h:i:s A'));

		// A bit of russian (I hope), to test UTF-8 file names.
		$zip->addFile("Привет мир!", "Кириллица имя файла.txt");
		$zip->addFile("Привет мир!", "Привет мир. С комментарий к файлу.txt", 0, "Кириллица файл комментарий");
		$zip->addFile("Hello World!", "hello.txt");

		@$handle = opendir($fileDir);
		if ($handle) {
			while (false !== ($file = readdir($handle))) {
				if (strpos($file, ".php") !== false) {
					$pathData = pathinfo($fileDir . $file);
					$fileName = $pathData['filename'];

					$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), null, true, ZipUtils::getFileExtAttr($file));
				}
			}
		}

		// Add a directory, first recursively, then the same directory, but without recursion.
		// Naturally this requires you to change the path to ../test to point to a directory of your own.
		// $zip->addDirectoryContent("testData/test", "recursiveDir/test");
		// $zip->addDirectoryContent("testData/test", "recursiveDir/testFlat", FALSE);

		//$zip->sendZip("ZipExample1.zip");
		//$zip->sendZip("ZipExample1_€2,000.zip");
		$zip->sendZip("ZipExample1_€2,000.zip", "application/zip", "ZipExample1_€2,000_utf8.zip");

	}