Last active
December 19, 2015 14:09
-
-
Save drewwalton19216801/5967104 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace Fuel\Tasks; | |
class Updatedb { | |
public function run() { | |
echo "Running cron job: updatedb\n"; | |
$devs = null; | |
$columns = array('username'); | |
$basedir = DOCROOT."uploads/devs/"; | |
echo "Base dir: ".$basedir."\n"; | |
$devs = \DB::select_array($columns)->from('users')->execute()->as_array(); | |
foreach ($devs as $dev) { | |
$devdir = $basedir.$dev['username']."/"; | |
$ite = new RecursiveDirectoryIterator($devdir, RecursiveDirectoryIterator::SKIP_DOTS); | |
// Iterate through each file | |
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { | |
// We found a file! | |
echo "$filename\n"; | |
// Check to see if file already is in database | |
$result = \DB::select('id', 'realpath', 'md5sum')->from('uploads')->where('realpath', $filename)->execute()->as_array(); | |
if ($result) { | |
// File found! See if md5sum needs updating. | |
$filemd5 = md5_file($filename); | |
if ($filemd5 == $result[0]['md5sum']) { | |
// md5sum is the same, skip this file | |
echo "md5 is the same!\n"; | |
} else { | |
// update md5sum | |
echo "md5 needs updating!\n"; | |
$result = \DB::update('uploads') | |
->value("md5sum", $filemd5) | |
->where('id', '=', $result[0]['id']) | |
->execute(); | |
echo 'md5 updated!\n'; | |
} | |
} else { | |
// We have a new file! Add it to the database. | |
$filemd5 = md5_file($filename); | |
$basefile = basename($filename); | |
\DB::insert('uploads')->columns(array( | |
'filename', 'userhandle', 'realpath', 'md5sum' | |
))->values(array( | |
$basefile, $dev['username'], $filename, $filemd5 | |
))->execute(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment