/
m_file.php
61 lines (50 loc) · 1.47 KB
/
m_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
<?php
class PtFile {
const BOOK_DIR = "books";
public function PtFile() {
}
public static function saveBook($bookId, $page, &$text) {
$bookDir = PtFile::getBookPath($bookId);
$bookPath = PtFile::getBookPath($bookId, $page);
if (!file_exists($bookDir)) {
mkdir($bookDir, 0777);
}
$fp = fopen("$bookPath", "w");
// save and convert to utf-16 for some reason
fputs($fp, iconv("UTF-8", "UTF-16", $text));
fclose($fp);
}
public static function deleteBook($bookId) {
$bookDir = $this->getBookPath($bookId);
PtFile::removeDir($bookDir);
}
public static function printBook($bookId, $page_from, $page_end)
{
for ($i = $page_from; $i <= $page_end; $i++) {
$bookPath = PtFile::getBookPath($bookId, $i);
if (file_exists($bookPath)) {
echo file_get_contents($bookPath);
}
}
}
public static function removeDir($directory)
{
foreach (glob("{$directory}/*") as $file)
{
if (is_dir($file)) {
PtFile::removeDir($file);
} else {
unlink($file);
}
}
rmdir($directory);
}
public static function getBookPath($bookId, $page = "") {
if ($page == "") {
return self::BOOK_DIR . "/$bookId";
} else {
return self::BOOK_DIR . "/$bookId/$page.txt";
}
}
}
?>