<?php

$root = "/data/violet/backup/resources";
$dbh = new PDO('mysql:host=210.211.97.53;dbname=tvtl', 'violet', '123456');
for ($cat = 0; $cat < 650; $cat++) {
  $path = "$root/$cat";
  if (!file_exists($path)) continue;
  foreach (scandir($path) as $dir) {
    if ($dir=='.' || $dir=='..' || !is_dir("$path/$dir") || !is_numeric($dir)) continue;
    $stmt = $dbh->query("select pr_category from tblpresentation where pr_id=$dir");
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($row == null) continue;
    list($pr_cat) = array_values($row);
    $dest = "/data/violet/backup/restemp/$pr_cat";
    if (!file_exists($dest)) mkdir($dest, 0777, true);
    echo("mv $path/$dir $dest\n");
    exec("mv $path/$dir $dest");
  }
}
