forked from NewEraCracker/php-work
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseful_functions.php
More file actions
95 lines (76 loc) · 1.97 KB
/
Copy pathuseful_functions.php
File metadata and controls
95 lines (76 loc) · 1.97 KB
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
<?php
/*
Author: NewEraCracker
License: Public Domain
*/
function readdir_recursive($dir, $show_dirs=false)
{
return explode("\r\n", readdir_recursive_string($dir, $show_dirs));
}
function readdir_recursive_string($dir, $show_dirs=false, $dir_len=null)
{
// Be sure about dir
$dir = realpath($dir);
// Calculate the length if we don't know it
if($dir_len == null) $dir_len = strlen($dir);
// Fun begins
ob_start();
// Open the dir
$dh = opendir($dir);
// Read the dir
while ( ( false !== ( $file = readdir($dh) ) ) && $dh )
{
if( $file != '.' && $file != '..')
{
$file = realpath($dir."/".$file);
if( is_dir($file) )
{
// If $show_dirs is true, dir names will also be listed
if( $show_dirs )
echo "\r\n".substr($file, $dir_len);
// Read recursively another dir below the original dir
// We pass $dir_len here so the path is relative to the 'mother' dir
echo "\r\n".readdir_recursive_string($file, $show_dirs, $dir_len);
}
else
{
// Just echo the filename
echo "\r\n".substr($file, $dir_len);
}
}
}
// Get the result
$contents = ob_get_contents();
// Clean the buffer
ob_end_clean();
// Return our result
return ltrim($contents);
}
function normalize_text_file($f)
{
// Bail out on error
if( !is_file($f) || !is_readable($f) || !is_writable($f) )
return;
// Grab contents and normalize them
$c = rtrim(convert_to_crlf(file_get_contents($f)));
// Explode the normalized contents
$c = explode("\r\n",$c);
// Build new contents while normalizing them
$new = '';
for($i=0;$i<count($c);$i++)
$new .= rtrim($c[$i])."\r\n";
$new = rtrim($new);
// Write the new contents to the file
file_put_contents($f,$new);
}
function convert_to_crlf($text)
{
// First, we take care of CRLF itself
$text = str_replace("\r\n","\n",$text);
// Then, we take care of any CR left
$text = str_replace("\r","\n",$text);
// Finally, we convert the LF back to CRLF
$text = str_replace("\n","\r\n",$text);
return $text;
}
?>