1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Includes/File.php. |
4
|
|
|
* |
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/Saibamen/Generate-Sort-Numbers |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Includes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Functions for operating on files. |
15
|
|
|
*/ |
16
|
|
|
class File |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Warn about overwriting file if exists and empty it. |
20
|
|
|
* |
21
|
|
|
* @param string $filename Filename without extension |
22
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
23
|
|
|
* |
24
|
|
|
* @see Input::dieOnDenyUserConfirm() |
25
|
|
|
*/ |
26
|
|
|
public static function checkIfFileExistsAndDeleteContent($filename, $fileExtension = '.dat') |
27
|
|
|
{ |
28
|
|
|
if (file_exists($filename.$fileExtension)) { |
29
|
|
|
Text::message('File '.$filename.$fileExtension.' exists and it will be overwritten!'); |
30
|
|
|
|
31
|
|
|
Input::dieOnDenyUserConfirm(); |
32
|
|
|
|
33
|
|
|
// Empty the file |
34
|
|
|
file_put_contents($filename.$fileExtension, ''); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create dir from filename if not exists. |
40
|
|
|
* |
41
|
|
|
* @param string $filename Filename |
42
|
|
|
*/ |
43
|
|
|
public static function createMissingDirectory($filename) |
44
|
|
|
{ |
45
|
|
|
if (!is_dir(dirname($filename))) { |
46
|
|
|
Text::debug('Creating missing directory: '.dirname($filename)); |
47
|
|
|
mkdir(dirname($filename)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get array from file content |
53
|
|
|
* |
54
|
|
|
* @param string $filename Filename without extension |
55
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
56
|
|
|
* @param string $delimiter Delimiter. Default is ' ' |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function getArrayFromFile($filename, $fileExtension = '.dat', $delimiter = ' ') |
61
|
|
|
{ |
62
|
|
|
$array = array(); |
63
|
|
|
|
64
|
|
|
if (file_exists($filename.$fileExtension)) { |
65
|
|
|
$array = explode($delimiter, file_get_contents($filename.$fileExtension)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $array; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|