23
23
*/
24
24
class File extends SplFileInfo
25
25
{
26
+ private $ filePath ;
27
+
28
+ public function __construct ( $ filePath = null )
29
+ {
30
+ if ( isset ( $ filePath ) ) {
31
+ $ this ->filePath = $ filePath ;
32
+ parent ::__construct ( $ filePath );
33
+ }
34
+ }
35
+
26
36
/**
27
37
* File::setGroup
28
38
*
@@ -297,6 +307,28 @@ public function read( $useIncludePath = false, $context = null )
297
307
298
308
// ------------------------------------------------------------------------
299
309
310
+ public function create ( $ filePath = null , $ mode = 'wb ' )
311
+ {
312
+ $ filePath = isset ( $ filePath ) ? $ filePath : $ this ->filePath ;
313
+ $ dir = dirname ( $ filePath );
314
+
315
+ if ( ! is_writable ( $ dir ) ) {
316
+ if ( ! file_exists ( $ dir ) ) {
317
+ mkdir ( $ dir , 0777 , true );
318
+ }
319
+ }
320
+
321
+ if ( ! $ fp = @fopen ( $ filePath , $ mode ) ) {
322
+ return false ;
323
+ }
324
+
325
+ parent ::__construct ( $ filePath );
326
+
327
+ return $ fp ;
328
+ }
329
+
330
+ // ------------------------------------------------------------------------
331
+
300
332
/**
301
333
* File::write
302
334
*
@@ -307,24 +339,24 @@ public function read( $useIncludePath = false, $context = null )
307
339
*
308
340
* @return bool
309
341
*/
310
- public function write ( $ contents , $ mode = 'wb ' )
342
+ public function write ( $ filePath , $ contents , $ mode = 'wb ' )
311
343
{
312
- if ( ! $ fp = @fopen ( $ this ->getRealPath (), $ mode ) ) {
313
- return false ;
314
- }
344
+ if ( false !== ( $ fp = $ this ->create ( $ filePath , $ mode ) ) ) {
345
+ flock ( $ fp , LOCK_EX );
315
346
316
- flock ( $ fp , LOCK_EX );
317
-
318
- for ( $ result = $ written = 0 , $ length = strlen ( $ contents ); $ written < $ length ; $ written += $ result ) {
319
- if ( ( $ result = fwrite ( $ fp , substr ( $ contents , $ written ) ) ) === false ) {
320
- break ;
347
+ for ( $ result = $ written = 0 , $ length = strlen ( $ contents ); $ written < $ length ; $ written += $ result ) {
348
+ if ( ( $ result = fwrite ( $ fp , substr ( $ contents , $ written ) ) ) === false ) {
349
+ break ;
350
+ }
321
351
}
322
- }
323
352
324
- flock ( $ fp , LOCK_UN );
325
- fclose ( $ fp );
353
+ flock ( $ fp , LOCK_UN );
354
+ fclose ( $ fp );
326
355
327
- return is_int ( $ result );
356
+ return is_int ( $ result );
357
+ }
358
+
359
+ return false ;
328
360
}
329
361
330
362
// ------------------------------------------------------------------------
0 commit comments