Skip to content

Instantly share code, notes, and snippets.

@joomdonation
Last active March 22, 2022 10:29
Show Gist options
  • Save joomdonation/94dd692cfd2d890259c9762ad1a2d45f to your computer and use it in GitHub Desktop.
Save joomdonation/94dd692cfd2d890259c9762ad1a2d45f to your computer and use it in GitHub Desktop.
public function delete_attachments()
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select('attachments')
->from('#__helpdeskpro_tickets')
->where('attachments != ""')
->where('YEAR(created_date) <= 2019');
$db->setQuery($query);
$messages = $db->loadObjectList();
$path = JPATH_ROOT . '/media/com_helpdeskpro/attachments/';
$count = 0;
foreach ($messages as $message)
{
if ($message->attachments)
{
$files = explode('|', $message->attachments);
foreach ($files as $file)
{
if (empty($file))
{
continue;
}
$filePath = $path . $file;
if (file_exists($filePath))
{
$count++;
//File::delete($filePath);
}
}
}
}
// Query messages
$query->clear()
->select('attachments')
->from('#__helpdeskpro_messages')
->where('attachments != ""')
->where('YEAR(date_added) <= 2019');
$db->setQuery($query);
$messages = $db->loadObjectList();
foreach ($messages as $message)
{
if ($message->attachments)
{
$files = explode('|', $message->attachments);
foreach ($files as $file)
{
if (empty($file))
{
continue;
}
$filePath = $path . $file;
if (file_exists($filePath))
{
$count++;
//File::delete($filePath);
}
}
}
}
echo $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment