File.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared;
  3. use InvalidArgumentException;
  4. use ZipArchive;
  5. class File
  6. {
  7. /**
  8. * Use Temp or File Upload Temp for temporary files.
  9. *
  10. * @var bool
  11. */
  12. protected static $useUploadTempDirectory = false;
  13. /**
  14. * Set the flag indicating whether the File Upload Temp directory should be used for temporary files.
  15. *
  16. * @param bool $useUploadTempDir Use File Upload Temporary directory (true or false)
  17. */
  18. public static function setUseUploadTempDirectory($useUploadTempDir)
  19. {
  20. self::$useUploadTempDirectory = (bool) $useUploadTempDir;
  21. }
  22. /**
  23. * Get the flag indicating whether the File Upload Temp directory should be used for temporary files.
  24. *
  25. * @return bool Use File Upload Temporary directory (true or false)
  26. */
  27. public static function getUseUploadTempDirectory()
  28. {
  29. return self::$useUploadTempDirectory;
  30. }
  31. /**
  32. * Verify if a file exists.
  33. *
  34. * @param string $pFilename Filename
  35. *
  36. * @return bool
  37. */
  38. public static function fileExists($pFilename)
  39. {
  40. // Sick construction, but it seems that
  41. // file_exists returns strange values when
  42. // doing the original file_exists on ZIP archives...
  43. if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
  44. // Open ZIP file and verify if the file exists
  45. $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
  46. $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
  47. $zip = new ZipArchive();
  48. if ($zip->open($zipFile) === true) {
  49. $returnValue = ($zip->getFromName($archiveFile) !== false);
  50. $zip->close();
  51. return $returnValue;
  52. }
  53. return false;
  54. }
  55. return file_exists($pFilename);
  56. }
  57. /**
  58. * Returns canonicalized absolute pathname, also for ZIP archives.
  59. *
  60. * @param string $pFilename
  61. *
  62. * @return string
  63. */
  64. public static function realpath($pFilename)
  65. {
  66. // Returnvalue
  67. $returnValue = '';
  68. // Try using realpath()
  69. if (file_exists($pFilename)) {
  70. $returnValue = realpath($pFilename);
  71. }
  72. // Found something?
  73. if ($returnValue == '' || ($returnValue === null)) {
  74. $pathArray = explode('/', $pFilename);
  75. while (in_array('..', $pathArray) && $pathArray[0] != '..') {
  76. $iMax = count($pathArray);
  77. for ($i = 0; $i < $iMax; ++$i) {
  78. if ($pathArray[$i] == '..' && $i > 0) {
  79. unset($pathArray[$i], $pathArray[$i - 1]);
  80. break;
  81. }
  82. }
  83. }
  84. $returnValue = implode('/', $pathArray);
  85. }
  86. // Return
  87. return $returnValue;
  88. }
  89. /**
  90. * Get the systems temporary directory.
  91. *
  92. * @return string
  93. */
  94. public static function sysGetTempDir()
  95. {
  96. if (self::$useUploadTempDirectory) {
  97. // use upload-directory when defined to allow running on environments having very restricted
  98. // open_basedir configs
  99. if (ini_get('upload_tmp_dir') !== false) {
  100. if ($temp = ini_get('upload_tmp_dir')) {
  101. if (file_exists($temp)) {
  102. return realpath($temp);
  103. }
  104. }
  105. }
  106. }
  107. return realpath(sys_get_temp_dir());
  108. }
  109. /**
  110. * Assert that given path is an existing file and is readable, otherwise throw exception.
  111. *
  112. * @param string $filename
  113. *
  114. * @throws InvalidArgumentException
  115. */
  116. public static function assertFile($filename)
  117. {
  118. if (!is_file($filename)) {
  119. throw new InvalidArgumentException('File "' . $filename . '" does not exist.');
  120. }
  121. if (!is_readable($filename)) {
  122. throw new InvalidArgumentException('Could not open "' . $filename . '" for reading.');
  123. }
  124. }
  125. }