-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathfileupload-checker-plain.php
More file actions
62 lines (53 loc) · 1.88 KB
/
Copy pathfileupload-checker-plain.php
File metadata and controls
62 lines (53 loc) · 1.88 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
<?php
//
// fileupload-checker-plain.php
//
// Simple file upload form that returns file size, hash and first 20 bytes
//
// Author: Emanuel Duss <me@emanuelduss.ch>
//
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload Tester</title>
</head>
<body>
<h1>File Upload Tester</h1>
<p>Upload a file to calculate its SHA256 sum, display its size, and show the first 20 bytes in hex:</p>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="file" name="file" required>
<br><br>
<input type="submit" value="Upload">
</form>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$fileSize = $_FILES['file']['size'];
$tmpFile = $_FILES['file']['tmp_name'];
if (is_uploaded_file($tmpFile)) {
$hashContext = hash_init('sha256');
$fileHandle = fopen($tmpFile, 'rb');
// Read the first 20 bytes to display in hex
$first20Bytes = fread($fileHandle, 20);
$hexFirst20Bytes = bin2hex($first20Bytes);
rewind($fileHandle);
// Read the file in chunks and update the hash context
while (!feof($fileHandle)) {
$chunk = fread($fileHandle, 8192); // Read in 8KB chunks
hash_update($hashContext, $chunk);
}
fclose($fileHandle);
$fileHash = hash_final($hashContext);
echo "<h3>File uploaded successfully!</h3>\n\n";
echo "<p>File Size: " . htmlspecialchars(number_format($fileSize)) . " bytes</p>\n";
echo "<p>SHA256 Hash: " . htmlspecialchars($fileHash, ENT_QUOTES, 'UTF-8') . "</p>\n";
echo "<p>First 20 Bytes (Hex): " . htmlspecialchars($hexFirst20Bytes, ENT_QUOTES, 'UTF-8') . "</p>\n";
} else {
echo "<h3>File upload failed.</h3>";
}
}
?>