Created
September 15, 2016 10:03
-
-
Save acenkus/839e8e5a58f6e372878568ccaa23610f to your computer and use it in GitHub Desktop.
Koala Framework delete unuseful uploads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Cli_CleanfilesControllerController extends Kwf_Controller_Action { | |
public function indexAction() { | |
//set memory and time for script | |
ini_set('memory_limit', "768M"); | |
set_time_limit(600); | |
//show current dir | |
echo "\nYour current directory: " . getcwd(); | |
//full path to uploads folder | |
$dir = getcwd() . "/uploads/"; | |
//show uploads path | |
echo "\nUploads folder: " . $dir; | |
$db = Kwf_Registry::get('db'); | |
//select all keys without relations | |
$sql = 'SELECT id | |
FROM kwf_uploads AS u | |
WHERE NOT EXISTS (SELECT NULL FROM documents WHERE u.id = picture_id ) | |
AND NOT EXISTS (SELECT NULL FROM books WHERE u.id = file_id ) | |
AND NOT EXISTS (SELECT NULL FROM employee WHERE u.id = picture_id ) | |
AND NOT EXISTS (SELECT NULL FROM flightFiles WHERE u.id = file_id ) | |
AND NOT EXISTS (SELECT NULL FROM tasks WHERE u.id = picture_id ) | |
AND NOT EXISTS (SELECT NULL FROM trainingContentQuestions WHERE u.id = picture_id ) | |
AND NOT EXISTS (SELECT NULL FROM trainingQuestions WHERE u.id = picture_id )'; | |
//get all rows | |
$fileRows = $db->query($sql)->fetchAll(); | |
if (count($fileRows) > 0) { | |
echo "\n\nRows count: " . count($fileRows); | |
$fileRowsMap = array_map(function($el) { return $el['id']; }, $fileRows); | |
$fileRowsStr = implode(',', $fileRowsMap); | |
//show string with ids to delete | |
echo "\nRows string: " . "\n" . $fileRowsStr . "\n"; | |
//delete each file | |
foreach ($fileRows as $file) { | |
echo "\nDB row " . $file['id']; | |
if (file_exists($dir . $file['id'])) { | |
echo "\nFile exists."; | |
if(unlink($dir . $file['id'])) { | |
echo "\nFile " . $file['id'] . " deleted successfully!" . PHP_EOL; | |
} | |
else { | |
echo "\nError. File " . $file['id'] . "not removed. " . PHP_EOL; | |
} | |
} else { | |
echo "\nFile does not exists." . PHP_EOL; | |
} | |
} | |
//delete rows from DB | |
$sql='DELETE FROM kwf_uploads WHERE id IN (' . $fileRowsStr . ')'; | |
$db->query($sql); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment