I only need to run it once so it makes no difference one way or the other but I'm just curious if there is anyway to improve it.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
set_time_limit(20000);
//DB credentials
$dbhost = '';
$dbname = '';
$dbuser = '';
$dbpasswd = '';
$link = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbname);
// Set the path for phpbb files folder
$files_root = $_SERVER['DOCUMENT_ROOT'] . '/forum/files';
//Uncomment to test directory
//echo $files_root;
//exit;
if ($handle = opendir($files_root))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != '.' && $entry != '..' && $entry != 'index.htm' && $entry != '.htaccess')
{
//Check if thumb itself is orphan
$strip_thumb = str_replace('thumb_', '', $entry);
$sql = "SELECT * FROM phpbb_attachments WHERE physical_filename = '" . $strip_thumb ."'";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) == 0)
{
// testing right now
//unlink($files_root . '/' . $entry);
echo $entry . '<br>';
}
mysqli_free_result($result);
}
}
closedir($handle);
}
?>
Done!