-
Notifications
You must be signed in to change notification settings - Fork 6
/
File.php
125 lines (115 loc) · 3.5 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
class File
{
public static function write($file, $data)
{
$fp = fopen($file, 'cb');
if (! $fp) {
return false;
}
if (! flock($fp, LOCK_EX | LOCK_NB)) {
fclose($fp);
return false;
}
if (fwrite($fp, $data) === false) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
if (! ftruncate($fp, mb_strlen($data))) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
public static function read($file)
{
$fp = fopen($file, 'rb');
if (! $fp) {
return false;
}
if (! flock($fp, LOCK_SH | LOCK_NB)) {
fclose($fp);
return false;
}
$data = stream_get_contents($fp);
if ($data === false) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
flock($fp, LOCK_UN);
fclose($fp);
return $data;
}
public static function down($file)
{
ob_start();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
public static function cut($file, $perLine = 100, $firstLineAsTitle = false, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$fileAttr = pathinfo($file);
$handle = fopen($file, 'rb');
if ($firstLineAsTitle) {
$title = fgetcsv($handle, 0, $delimiter, $enclosure, $escape);
}
for ($i = 0; $row = fgetcsv($handle, 0, $delimiter, $enclosure, $escape); $i ++) {
if ($i % $perLine == 0) {
$fp = fopen($fileAttr['filename'] . '_' . $i / $perLine . '.' . $fileAttr['extension'], 'cb');
if ($firstLineAsTitle) {
fputcsv($fp, $title);
}
}
fputcsv($fp, $row);
}
fclose($handle);
}
public static function createDir($path, $rights = 0777)
{
if (is_dir($path))
return true;
$result = @mkdir($path, $rights, true);
@chmod($path, $rights);
return $result;
}
public static function findFolder($dir)
{
$directory = glob($dir . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
for ($i = 0; $i < count($directory); $i ++) {
$add = glob($directory[$i] . '/*', GLOB_ONLYDIR | GLOB_NOSORT);
if (count($add) > 0) {
$directory = array_merge($directory, $add);
}
}
return array_merge(array(
$dir
), $directory);
}
public static function findFiles($dir, $extensions = '*.*')
{
$directory = self::findFolder($dir);
$files = array();
foreach ($directory as $v) {
$add = glob($v . '/{' . $extensions . '}', GLOB_BRACE | GLOB_NOSORT);
if (count($add) > 0) {
$files = array_merge($files, $add);
}
}
return $files;
}
}