open($zipFile); if ($res === true) { $returnValue = ($zip->getFromName($archiveFile) !== false); $zip->close(); return $returnValue; } } return false; } return file_exists($filename); } /** * Returns canonicalized absolute pathname, also for ZIP archives. */ public static function realpath(string $filename): string { // Returnvalue $returnValue = ''; // Try using realpath() if (file_exists($filename)) { $returnValue = realpath($filename) ?: ''; } // Found something? if ($returnValue === '') { $pathArray = explode('/', $filename); while (in_array('..', $pathArray) && $pathArray[0] != '..') { $iMax = count($pathArray); for ($i = 0; $i < $iMax; ++$i) { if ($pathArray[$i] == '..' && $i > 0) { unset($pathArray[$i], $pathArray[$i - 1]); break; } } } $returnValue = implode('/', $pathArray); } // Return return $returnValue; } /** * Get the systems temporary directory. */ public static function sysGetTempDir(): string { $path = sys_get_temp_dir(); if (self::$useUploadTempDirectory) { // use upload-directory when defined to allow running on environments having very restricted // open_basedir configs if (ini_get('upload_tmp_dir') !== false) { if ($temp = ini_get('upload_tmp_dir')) { if (file_exists($temp)) { $path = $temp; } } } } return realpath($path) ?: ''; } public static function temporaryFilename(): string { $filename = tempnam(self::sysGetTempDir(), 'phpspreadsheet'); if ($filename === false) { throw new Exception('Could not create temporary file'); } return $filename; } /** * Assert that given path is an existing file and is readable, otherwise throw exception. */ public static function assertFile(string $filename, string $zipMember = ''): void { if (!is_file($filename)) { throw new ReaderException('File "' . $filename . '" does not exist.'); } if (!is_readable($filename)) { throw new ReaderException('Could not open "' . $filename . '" for reading.'); } if ($zipMember !== '') { $zipfile = "zip://$filename#$zipMember"; if (!self::fileExists($zipfile)) { throw new ReaderException("Could not find zip member $zipfile"); } } } /** * Same as assertFile, except return true/false and don't throw Exception. */ public static function testFileNoThrow(string $filename, ?string $zipMember = null): bool { if (!is_file($filename)) { return false; } if (!is_readable($filename)) { return false; } if ($zipMember === null) { return true; } // validate zip, but don't check specific member if ($zipMember === '') { return self::validateZipFirst4($filename); } return self::fileExists("zip://$filename#$zipMember"); } }