-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.php
More file actions
82 lines (67 loc) · 2 KB
/
Copy pathhandler.php
File metadata and controls
82 lines (67 loc) · 2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Reader</title>
<style>
* {
font-family: 'Quicksand';
font-size: xx-large;
}
body {
font-family: Arial, sans-serif;
padding: 0 10% 0 10%;
}
.success {
color: green;
}
.error {
color: red;
}
textarea {
width: 100%;
height: 200px;
border-radius: 7px;
}
</style>
</head>
<body>
<h1>File Reader</h1>
<form method="post">
<label for="filename">Enter filename:</label>
<input style="border-radius: 5px; border: .5px solid black;" type="text" name="filename" id="filename" required>
<button style="border-radius: 5px; border: .5px solid black;" type="submit">Read File</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$file = $_POST['filename'];
function readFileContent($file)
{
if (!file_exists($file)) {
throw new Exception("File '$file' does not exist ✗");
}
if (!is_file($file)) {
throw new Exception("Path is not a file");
}
$content = file_get_contents($file);
if ($content === false) {
throw new Exception("Failed to read '$file' ✗");
}
if (filesize($file) === 0) {
throw new Exception("'$file' is empty!");
}
return $content;
}
try {
$content = readFileContent($file);
echo "<h2 class='success'>File content:</h2>";
echo "<textarea readonly>" . htmlspecialchars($content) . "</textarea>";
} catch (Exception $e) {
echo "<p class='error'>Error: " . htmlspecialchars($e->getMessage()) . "</p>";
} finally {
echo "<p>Done trying to read the file.</p>";
}
}
?>
</body>
</html>