Python Duplicate Image Cleanup and PHP File Protection Examples
These examples are shortened public versions of patterns commonly used in personal projects. Replace the placeholders (values such as paths that must match your own setup) and they work right away; in production, adapt authentication, logging, backups, and permission checks to your environment.
Python SHA-256 Duplicate Check
from pathlib import Path
import hashlib
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
def file_hash(path: Path) -> str:
# Read the file in 1 MB chunks and compute a SHA-256 hash (content fingerprint)
h = hashlib.sha256()
with path.open("rb") as f:
for chunk in iter(lambda: f.read(1024 * 1024), b""):
h.update(chunk)
return h.hexdigest()
def find_duplicates(root: str):
seen = {}
duplicates = []
for path in Path(root).rglob("*"):
if not path.is_file() or path.suffix.lower() not in IMAGE_EXTS:
continue # check image extensions only
digest = file_hash(path)
if digest in seen:
duplicates.append((seen[digest], path)) # (original, duplicate candidate)
else:
seen[digest] = path # remember the first hash as the original
return duplicates
for original, duplicate in find_duplicates("./uploads"):
print(f"{duplicate} == {original}")
PHP Private Media Router
<?php
session_start();
if (empty($_SESSION["user_id"])) {
http_response_code(404); // respond as if the file does not exist when not logged in
exit;
}
$rel = ltrim($_GET["file"] ?? "", "/");
$path = realpath(__DIR__ . "/private_uploads/" . $rel);
$root = realpath(__DIR__ . "/private_uploads");
if (!$path || !str_starts_with($path, $root . DIRECTORY_SEPARATOR)) {
http_response_code(404); // block paths outside the protected folder (stops ../ tricks)
exit;
}
header("Content-Type: " . mime_content_type($path));
header("X-Robots-Tag: noindex, nofollow"); // keep search engines away
readfile($path);
Nginx Direct Access Block
# Block direct web access to the protected folders
location ~ ^/(private_uploads|logs|data)/ {
deny all;
return 404;
}
# Files are served only through media.php, which checks authentication
location = /media.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}