30
30
*/
31
31
class PHPWord_Shared_File
32
32
{
33
+ /*
34
+ * Use Temp or File Upload Temp for temporary files
35
+ *
36
+ * @protected
37
+ * @var boolean
38
+ */
39
+ protected static $ _useUploadTempDirectory = FALSE ;
40
+
41
+
42
+ /**
43
+ * Set the flag indicating whether the File Upload Temp directory should be used for temporary files
44
+ *
45
+ * @param boolean $useUploadTempDir Use File Upload Temporary directory (true or false)
46
+ */
47
+ public static function setUseUploadTempDirectory ($ useUploadTempDir = FALSE ) {
48
+ self ::$ _useUploadTempDirectory = (boolean ) $ useUploadTempDir ;
49
+ } // function setUseUploadTempDirectory()
50
+
51
+
52
+ /**
53
+ * Get the flag indicating whether the File Upload Temp directory should be used for temporary files
54
+ *
55
+ * @return boolean Use File Upload Temporary directory (true or false)
56
+ */
57
+ public static function getUseUploadTempDirectory () {
58
+ return self ::$ _useUploadTempDirectory ;
59
+ } // function getUseUploadTempDirectory()
60
+
33
61
/**
34
62
* Verify if a file exists
35
63
*
36
- * @param string $pFilename Filename
37
- * @return bool
64
+ * @param string $pFilename Filename
65
+ * @return boolean
38
66
*/
39
67
public static function file_exists ($ pFilename )
40
68
{
@@ -45,7 +73,7 @@ public static function file_exists($pFilename)
45
73
/**
46
74
* Returns canonicalized absolute pathname, also for ZIP archives
47
75
*
48
- * @param string $pFilename
76
+ * @param string $pFilename
49
77
* @return string
50
78
*/
51
79
public static function realpath ($ pFilename )
@@ -74,4 +102,70 @@ public static function realpath($pFilename)
74
102
// Return
75
103
return $ returnValue ;
76
104
}
105
+
106
+ /**
107
+ * Return the Image Type from a file
108
+ *
109
+ * @param string $filename
110
+ * @return return
111
+ */
112
+ public static function imagetype ($ filename ) {
113
+ if (function_exists ('exif_imagetype ' )) {
114
+ return exif_imagetype ($ filename );
115
+ } else {
116
+ if ((list ($ width , $ height , $ type , $ attr ) = getimagesize ( $ filename )) !== false ) {
117
+ return $ type ;
118
+ }
119
+ }
120
+ return false ;
121
+ }
122
+
123
+ /**
124
+ * Get the systems temporary directory.
125
+ *
126
+ * @return string
127
+ */
128
+ public static function sys_get_temp_dir ()
129
+ {
130
+ if (self ::$ _useUploadTempDirectory ) {
131
+ // use upload-directory when defined to allow running on environments having very restricted
132
+ // open_basedir configs
133
+ if (ini_get ('upload_tmp_dir ' ) !== FALSE ) {
134
+ if ($ temp = ini_get ('upload_tmp_dir ' )) {
135
+ if (file_exists ($ temp ))
136
+ return realpath ($ temp );
137
+ }
138
+ }
139
+ }
140
+
141
+ // sys_get_temp_dir is only available since PHP 5.2.1
142
+ // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
143
+ if ( !function_exists ('sys_get_temp_dir ' )) {
144
+ if ($ temp = getenv ('TMP ' ) ) {
145
+ if ((!empty ($ temp )) && (file_exists ($ temp ))) { return realpath ($ temp ); }
146
+ }
147
+ if ($ temp = getenv ('TEMP ' ) ) {
148
+ if ((!empty ($ temp )) && (file_exists ($ temp ))) { return realpath ($ temp ); }
149
+ }
150
+ if ($ temp = getenv ('TMPDIR ' ) ) {
151
+ if ((!empty ($ temp )) && (file_exists ($ temp ))) { return realpath ($ temp ); }
152
+ }
153
+
154
+ // trick for creating a file in system's temporary dir
155
+ // without knowing the path of the system's temporary dir
156
+ $ temp = tempnam (__FILE__ , '' );
157
+ if (file_exists ($ temp )) {
158
+ unlink ($ temp );
159
+ return realpath (dirname ($ temp ));
160
+ }
161
+
162
+ return null ;
163
+ }
164
+
165
+ // use ordinary built-in PHP function
166
+ // There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
167
+ // be called if we're running 5.2.1 or earlier
168
+ return realpath (sys_get_temp_dir ());
169
+ }
170
+
77
171
}
0 commit comments