-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandling.php
160 lines (129 loc) · 4.3 KB
/
filehandling.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class FileHandling {
function createFile($filePath){
$handle = fopen($filePath, "r+");
fclose($handle);
}
//file and script should be in the same path
function getFileAbsolutePath($file){
return pathinfo(realpath($file), PATHINFO_DIRNAME);
}
//$filePath should be absolute
function unzipFile($filePath){
$path = $this->getFileAbsolutePath($filePath);
$zip = new ZipArchive;
$res = $zip->open($filePath);
if($res === TRUE){
//$path should be file destination folder
$zip->extractTo($path);
$zip->close();
echo "successfully extracted";
}else{
throw new Exception("could not open file");
}
}
function copyFile($original, $target){
$result = copy($original, $target);
if($result == 1){
return true;
}else {
return false;
}
}
function copyContentofFileandRename($filePath, $rename){
}
function checkIfaFileContainsinFolder($filePath){
if(file_exists($filePath)){
return true;
}else{
return false;
}
}
function listAllFoldersinFolder($dir){
$dirs = array_filter(glob('*'), 'is_dir');
foreach($dirs as $dir){
echo $dir;
}
}
function renameFile($fileName, $newFileName){
rename($fileName, $newFileName);
}
//fopen会覆盖另一个
static function writetoFile($filename, $content){
$file = fopen($filename, 'w');
fwrite($file, $content);
fclose($file);
}
static function writetoFileLinebyLine($filePath, $linecontent){
$file = fopen($filePath,"a");
fwrite($file,$linecontent.PHP_EOL);
fclose($file);
}
//从含某个字符串的某行开始删除文件
//删除某行的代码
function removeLine($linenumber){
}
static function readingFileLinebyLine($filePath){
$fp = fopen($filePath, "r");
if($fp == false){
exit();
}
while(!feof($fp)){
echo fgets($fp)."<br>";
}
}
//put this in a loop to remove line of text
function removeALinefromFile($filePath, $line){
file_put_contents($filePath, str_replace($line, "", file_get_contents($filePath)));
}
function replaceALinefromFile($filePath, $line, $replace){
file_put_contents($filePath, str_replace($line, $replace, file_get_contents($filePath)));
}
function insertCertainLineBeforeSpecificLine($filePath,$searchneedle,$secondneedle,$linecontent){
$handle = fopen($filePath, "+r") or die("could not open file");
$linenumber = 0;
while(!feof($handle)){
$line = fgets($handle);
$linenumber++;
if(strpos($line,$searchneedle)){
$number = $linenumber;
}
if(isset($number)){
if($linenumber > $number){
if(strpos($line,$secondneedle)){
file_put_contents($filePath, str_replace($line, $secondneedle, file_get_contents($filePath)));
}
}
}
}
}
function appendLineToFile($filePath, $line){
$handle = fopen($filePath, 'a') or die("cannot open file: ".$filePath);
$newline = "\n".$line;
fwrite($handle, $newline);
}
function searchReplaceStringinFile($search, $replace, $filePath){
$str=file_get_contents($filePath);
$str=str_replace($search, $replace,$str);
file_put_contents($filePath, $str);
}
function writeToStartofFile($filepath, $content){
$content .= file_get_contents($filepath);
file_put_contents($filepath, $content);
}
//copy files in a folder to another folder
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
}