/
file-list.php
122 lines (113 loc) · 3.39 KB
/
file-list.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
<?php
if(isset($_POST["edit_file"])) {
file_put_contents($_POST["edit_file"], $_POST["edit_text"]);
}
if(isset($_POST["add_name"])) {
file_put_contents("files/".$_POST["add_name"], $_POST["text"]);
header("Location: file-list.php");
}
if(isset($_GET["delete"])) {
@unlink("files/".$_GET["delete"]);
header("Location: file-list.php");
}
function filesList($dir, $removeFile) // Путь к директории, Массив запрещеных файлов
{
$html = '';
if (is_dir($dir)) {
if ($dh = opendir($dir))
{
$html .= '<table>'. PHP_EOL;
$i = 0;
while (($file = readdir($dh)) !== false) {
if (!in_array($file, $removeFile)) {
$html .= "<tr>". PHP_EOL;
$html .= "<td>".++$i."</td>". PHP_EOL;
$html .= "<td><a href=\"?view={$file}\">{$file}</a></td>". PHP_EOL;
$html .= "<td><a href=\"?edit={$file}\">Редактировать</a></td>". PHP_EOL;
$html .= "<td><a href=\"?delete={$file}\">Удалить</a></td>". PHP_EOL;
$html .= "</tr>". PHP_EOL;
}
}
$html .= '<table>'. PHP_EOL;
closedir($dh);
}
echo $html;
return;
}
else
{
echo "Ошибка";
return;
}
}
?><!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
table {
border-collapse: collapse;
}
td {
padding: 5px;
border: 1px solid #999;
}
</style>
</head>
<body>
<?if(isset($_GET["add"])):?>
<form method="post" action="">
<table>
<tr>
<td>
Название файла
</td>
<td>
<input type="text" name="add_name">
</td>
</tr>
<tr>
<td>
Содержимаое файла
</td>
<td><textarea name="text" cols="30" rows="10"><?=$text?></textarea></td>
</tr>
<tr>
<td></td><td><input type="submit" value="Сохранить"></td>
</tr>
</table>
<p><a href="file-list.php">Вернуться к списку</a></p>
</form>
<?elseif(isset($_GET["view"])):?>
<?
$fileName = $_GET["view"];
$text = file_get_contents("files/".$fileName);
echo $text;
?>
<p><a href="file-list.php">Вернуться к списку</a></p>
<?elseif(isset($_GET["edit"])):?>
<?
$fileName = $_GET["edit"];
$text = file_get_contents("files/".$fileName);
?>
<form method="post" action="">
<table>
<tr>
<td>
<textarea name="edit_text" cols="30" rows="10"><?=$text?></textarea>
<input type="hidden" name="edit_file" value="files/<?=$fileName?>">
</td>
</tr>
<tr>
<td><input type="submit" value="Сохранить"></td>
</tr>
</table>
<p><a href="file-list.php">Вернуться к списку</a></p>
</form>
<?else:?>
<p><a href="?add">Добавить файл</a></p>
<?filesList("files/", array('.', '..'));?>
<?endif;?>
</body>
</html>