/
index5.php
43 lines (33 loc) · 976 Bytes
/
index5.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
// Directory functions.
<?php
$root = './books';
$dir = opendir($root);
$library = [];
while(($author = readdir($dir)) != FALSE) {
if($author == '.' || $author == '..') {
continue;
}
$books = scandir("$root/$author");
foreach($books as $title) {
if(is_dir($title)) {
continue;
}
$library[] = [$author, substr($title, 0, -4)];
}
}
closedir($dir);
echo "Which book would you like to read? \n\n";
foreach($library as $idx => $book) {
$option = $idx + 1;
echo "$option) $book[1] by $book[0] \n";
}
list($option) = fscanf(STDIN, "%d \n");
list($author, $title) = $library[$option - 1];
$filename = "$root/$author/$title.txt";
$f = fopen($filename, 'r');
while(!feof($f)) {
echo fread($f, 512);
fgets(STDIN);
}
fclose($f);
?>