I have a simple PHP script that queries MySQL DB and deletes(unlinks) file(s) from an uploads folder when a condition (expiry timestamp) is met. Then it redirects to another PHP script. I have set up a cron job to run the first script automatically, this works fine and deletes the files where needed from the uploads folder, but it does not redirect when my if-else statement gets triggered. If I manually paste the file path to the browser, the script runs, deletes and redirects as I would expect. (Also will not redirect if I run the PHP file from the CLI). Any help would be much appreciated.
include_once 'db.php';
date_default_timezone_set("Asia/Bangkok");
$date = date('Y-m-d H:i:s');
$sql = "SELECT name FROM files WHERE expiry <= '$date'";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo('Could not get data: '.mysqli_error());
}
while ($row = mysqli_fetch_array($result)) {
$delete = ("assets/uploads/{$row['name']}");
if (file_exists($delete)) {
unlink($delete);
} else {
header("Location: expire.php",
true, 301);
exit();
}
}
mysqli_close($conn);