File.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Shared_File
  8. {
  9. /*
  10. * Use Temp or File Upload Temp for temporary files
  11. *
  12. * @protected
  13. * @var boolean
  14. */
  15. protected static $useUploadTempDirectory = false;
  16. /**
  17. * Set the flag indicating whether the File Upload Temp directory should be used for temporary files
  18. *
  19. * @param boolean $useUploadTempDir Use File Upload Temporary directory (true or false)
  20. */
  21. public static function setUseUploadTempDirectory($useUploadTempDir = false)
  22. {
  23. self::$useUploadTempDirectory = (boolean) $useUploadTempDir;
  24. }
  25. /**
  26. * Get the flag indicating whether the File Upload Temp directory should be used for temporary files
  27. *
  28. * @return boolean Use File Upload Temporary directory (true or false)
  29. */
  30. public static function getUseUploadTempDirectory()
  31. {
  32. return self::$useUploadTempDirectory;
  33. }
  34. /**
  35. * Verify if a file exists
  36. *
  37. * @param string $pFilename Filename
  38. * @return bool
  39. */
  40. public static function file_exists($pFilename)
  41. {
  42. // Sick construction, but it seems that
  43. // file_exists returns strange values when
  44. // doing the original file_exists on ZIP archives...
  45. if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
  46. // Open ZIP file and verify if the file exists
  47. $zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
  48. $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
  49. $zip = new ZipArchive();
  50. if ($zip->open($zipFile) === true) {
  51. $returnValue = ($zip->getFromName($archiveFile) !== false);
  52. $zip->close();
  53. return $returnValue;
  54. } else {
  55. return false;
  56. }
  57. } else {
  58. // Regular file_exists
  59. return file_exists($pFilename);
  60. }
  61. }
  62. /**
  63. * Returns canonicalized absolute pathname, also for ZIP archives
  64. *
  65. * @param string $pFilename
  66. * @return string
  67. */
  68. public static function realpath($pFilename)
  69. {
  70. // Returnvalue
  71. $returnValue = '';
  72. // Try using realpath()
  73. if (file_exists($pFilename)) {
  74. $returnValue = realpath($pFilename);
  75. }
  76. // Found something?
  77. if ($returnValue == '' || ($returnValue === null)) {
  78. $pathArray = explode('/', $pFilename);
  79. while (in_array('..', $pathArray) && $pathArray[0] != '..') {
  80. for ($i = 0; $i < count($pathArray); ++$i) {
  81. if ($pathArray[$i] == '..' && $i > 0) {
  82. unset($pathArray[$i]);
  83. unset($pathArray[$i - 1]);
  84. break;
  85. }
  86. }
  87. }
  88. $returnValue = implode('/', $pathArray);
  89. }
  90. // Return
  91. return $returnValue;
  92. }
  93. /**
  94. * Get the systems temporary directory.
  95. *
  96. * @return string
  97. */
  98. public static function sys_get_temp_dir()
  99. {
  100. if (self::$useUploadTempDirectory) {
  101. // use upload-directory when defined to allow running on environments having very restricted
  102. // open_basedir configs
  103. if (ini_get('upload_tmp_dir') !== false) {
  104. if ($temp = ini_get('upload_tmp_dir')) {
  105. if (file_exists($temp)) {
  106. return realpath($temp);
  107. }
  108. }
  109. }
  110. }
  111. // sys_get_temp_dir is only available since PHP 5.2.1
  112. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  113. if (!function_exists('sys_get_temp_dir')) {
  114. if ($temp = getenv('TMP')) {
  115. if ((!empty($temp)) && (file_exists($temp))) {
  116. return realpath($temp);
  117. }
  118. }
  119. if ($temp = getenv('TEMP')) {
  120. if ((!empty($temp)) && (file_exists($temp))) {
  121. return realpath($temp);
  122. }
  123. }
  124. if ($temp = getenv('TMPDIR')) {
  125. if ((!empty($temp)) && (file_exists($temp))) {
  126. return realpath($temp);
  127. }
  128. }
  129. // trick for creating a file in system's temporary dir
  130. // without knowing the path of the system's temporary dir
  131. $temp = tempnam(__FILE__, '');
  132. if (file_exists($temp)) {
  133. unlink($temp);
  134. return realpath(dirname($temp));
  135. }
  136. return null;
  137. }
  138. // use ordinary built-in PHP function
  139. // There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
  140. // be called if we're running 5.2.1 or earlier
  141. return realpath(sys_get_temp_dir());
  142. }
  143. }