Scans all files and matches them with the reference file in DB then sets the file size and Mime Type. Note this update doesnt do any DB Structure changes

This commit is contained in:
johnnyq 2024-10-30 19:17:21 -04:00
parent 241b0008e8
commit 70569fc226
3 changed files with 132 additions and 4 deletions

70
admin_fix.php Normal file
View File

@ -0,0 +1,70 @@
<?php
/*
* ITFlow - Custom script to fill in DB Values Size and MIME Type for uploaded files
*/
require_once "config.php";
require_once "functions.php";
require_once "check_login.php";
function scanDirectory($dir, $mysqli) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if ($file->isFile()) {
$file_path = $file->getPathname();
$file_name = $file->getFilename();
// Process the file
processFile($file_path, $file_name, $mysqli);
}
}
}
function processFile($file_path, $file_name, $mysqli) {
// Get the file size
$file_size = filesize($file_path);
// Get the MIME type
$file_mime_type = mime_content_type($file_path);
// Prepare a statement to check if the file exists in the database
$stmt_select = mysqli_prepare($mysqli, "SELECT file_id FROM files WHERE file_reference_name = ?");
mysqli_stmt_bind_param($stmt_select, 's', $file_name);
mysqli_stmt_execute($stmt_select);
mysqli_stmt_store_result($stmt_select);
if (mysqli_stmt_num_rows($stmt_select) > 0) {
// File exists in the database, proceed to update
$stmt_update = mysqli_prepare($mysqli, "UPDATE files SET file_mime_type = ?, file_size = ? WHERE file_reference_name = ?");
mysqli_stmt_bind_param($stmt_update, 'sis', $file_mime_type, $file_size, $file_name);
if (mysqli_stmt_execute($stmt_update)) {
echo "Updated: $file_name\n";
} else {
echo "Error updating $file_name: " . mysqli_stmt_error($stmt_update) . "\n";
}
mysqli_stmt_close($stmt_update);
} else {
echo "No database entry found for: $file_name\n";
}
mysqli_stmt_close($stmt_select);
}
// Define the uploads directory (modify the path if necessary)
$uploads_dir = __DIR__ . '/uploads';
// Start scanning from the uploads directory
scanDirectory($uploads_dir, $mysqli);
// Close the database connection
mysqli_close($mysqli);
$_SESSION['alert_message'] = "Files Fixed";
header("Location: " . $_SERVER["HTTP_REFERER"]);

View File

@ -2364,10 +2364,68 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) {
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.3'");
}
// if (CURRENT_DATABASE_VERSION == '1.6.3') {
// // Insert queries here required to update to DB version 1.6.4
if (CURRENT_DATABASE_VERSION == '1.6.3') {
// Find Files and update the Mime Type and File Size
function scanDirectory($dir, $mysqli) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if ($file->isFile()) {
$file_path = $file->getPathname();
$file_name = $file->getFilename();
// Process the file
processFile($file_path, $file_name, $mysqli);
}
}
}
function processFile($file_path, $file_name, $mysqli) {
// Get the file size
$file_size = filesize($file_path);
// Get the MIME type
$file_mime_type = mime_content_type($file_path);
// Prepare a statement to check if the file exists in the database
$stmt_select = mysqli_prepare($mysqli, "SELECT file_id FROM files WHERE file_reference_name = ?");
mysqli_stmt_bind_param($stmt_select, 's', $file_name);
mysqli_stmt_execute($stmt_select);
mysqli_stmt_store_result($stmt_select);
if (mysqli_stmt_num_rows($stmt_select) > 0) {
// File exists in the database, proceed to update
$stmt_update = mysqli_prepare($mysqli, "UPDATE files SET file_mime_type = ?, file_size = ? WHERE file_reference_name = ?");
mysqli_stmt_bind_param($stmt_update, 'sis', $file_mime_type, $file_size, $file_name);
if (mysqli_stmt_execute($stmt_update)) {
echo "Updated: $file_name\n";
} else {
echo "Error updating $file_name: " . mysqli_stmt_error($stmt_update) . "\n";
}
mysqli_stmt_close($stmt_update);
} else {
echo "No database entry found for: $file_name\n";
}
mysqli_stmt_close($stmt_select);
}
// Define the uploads directory (modify the path if necessary)
$uploads_dir = __DIR__ . '/uploads';
// Start scanning from the uploads directory
scanDirectory($uploads_dir, $mysqli);
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.4'");
}
// if (CURRENT_DATABASE_VERSION == '1.6.4') {
// // Insert queries here required to update to DB version 1.6.5
// // Then, update the database to the next sequential version
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.4'");
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.6.5'");
// }
} else {

View File

@ -5,4 +5,4 @@
* It is used in conjunction with database_updates.php
*/
DEFINE("LATEST_DATABASE_VERSION", "1.6.3");
DEFINE("LATEST_DATABASE_VERSION", "1.6.4");