Limit the images that previews keep.

A preview that fails the captcha or the form token keeps no image. A session keeps one image, the directory has a size limit, and abandoned images go at the next upload.
This commit is contained in:
tim
2026-09-26 15:27:51 +02:00
parent fe30a0aaa1
commit c4fcef21aa
8 changed files with 215 additions and 42 deletions
+7 -19
View File
@@ -121,7 +121,7 @@ function save_data($id = NULL) {
// Prune filesystem
if ($image_file)
prune_images($image_file);
prune_images();
// email admin if added to the queue
if ($action == 'submit')
@@ -156,9 +156,9 @@ function get_mimetype($image_file) {
}
function save_uploaded_file() {
// A new upload in this request
// A new upload in this request: PHP removes the file after the request
if (is_uploaded_file($_FILES['upload']['tmp_name'] ?? '')) {
return store_upload($_FILES['upload']['tmp_name']);
return $_FILES['upload']['tmp_name'];
}
// An image uploaded at the preview step; the session knows which one
if (($_POST['uploaded'] ?? '') === 'pending') {
@@ -167,22 +167,10 @@ function save_uploaded_file() {
return FALSE;
}
function prune_images($image_file) {
// Delete the saved image, and anything older than $age.
// Only ever touch upload files inside the upload directory.
$age = 86400; // a day old
$dir = upload_dir();
if (dirname($image_file) === $dir && is_upload_name(basename($image_file))) {
@unlink($image_file);
}
unset($_SESSION['pending_upload']);
foreach (glob($dir . '/*') as $cleanup_file) {
if (is_upload_name(basename($cleanup_file)) &&
@filemtime($cleanup_file) < (time() - $age)) {
@unlink($cleanup_file);
}
}
// After a save: remove this session's upload, and any abandoned uploads
function prune_images() {
remove_pending_upload();
prune_uploads();
}
/*