@@ -155,13 +155,16 @@ public static function getAutoClass($config)
155155 */
156156 public static function getPath ($ skip_create_path = false , $ config )
157157 {
158- if (! isset ( $ config [ ' path ' ]) || $ config [ ' path ' ] == '' ) {
158+ $ tmp_dir = ini_get ( ' upload_tmp_dir ' ) ? ini_get ( ' upload_tmp_dir ' ) : sys_get_temp_dir ();
159159
160+ if (!isset ($ config [ 'path ' ]) || $ config [ 'path ' ] == '' ) {
160161 if (self ::isPHPModule ()) {
161- $ tmp_dir = ini_get ('upload_tmp_dir ' ) ? ini_get ('upload_tmp_dir ' ) : sys_get_temp_dir ();
162162 $ path = $ tmp_dir ;
163163 } else {
164- $ path = isset ($ _SERVER [ 'DOCUMENT_ROOT ' ]) ? rtrim ($ _SERVER [ 'DOCUMENT_ROOT ' ], '/ ' ) . '/../ ' : rtrim (__DIR__ , '/ ' ) . '/ ' ;
164+ $ document_root_path = rtrim ($ _SERVER [ 'DOCUMENT_ROOT ' ], '/ ' ) . '/../ ' ;
165+ $ path = isset ($ _SERVER [ 'DOCUMENT_ROOT ' ]) && is_writable ($ document_root_path )
166+ ? $ document_root_path
167+ : rtrim (__DIR__ , '/ ' ) . '/ ' ;
165168 }
166169
167170 if (self ::$ config [ 'path ' ] != '' ) {
@@ -187,7 +190,7 @@ public static function getPath($skip_create_path = false, $config)
187190
188191 $ securityKey = self ::cleanFileName ($ securityKey );
189192
190- $ full_path = $ path . '/ ' . $ securityKey ;
193+ $ full_path = rtrim ( $ path, ' / ' ) . '/ ' . $ securityKey ;
191194 $ full_pathx = md5 ($ full_path );
192195
193196
@@ -200,6 +203,16 @@ public static function getPath($skip_create_path = false, $config)
200203 if (!@is_writable ($ full_path )) {
201204 @chmod ($ full_path , self ::__setChmodAuto ($ config ));
202205 }
206+ if (!@is_writable ($ full_path )) {
207+ // switch back to tmp dir again if the path is not writeable
208+ $ full_path = rtrim ($ tmp_dir ,'/ ' ) . '/ ' . $ securityKey ;
209+ if (!@file_exists ($ full_path )) {
210+ @mkdir ($ full_path , self ::__setChmodAuto ($ config ));
211+ }
212+ if (!@is_writable ($ full_path )) {
213+ @chmod ($ full_path , self ::__setChmodAuto ($ config ));
214+ }
215+ }
203216 if (!@file_exists ($ full_path ) || !@is_writable ($ full_path )) {
204217 throw new phpFastCacheCoreException ('PLEASE CREATE OR CHMOD ' . $ full_path . ' - 0777 OR ANY WRITABLE PERMISSION! ' , 92 );
205218 }
0 commit comments