<?php
$fujian = $_FILES['fujian'];
$fileData = array();
$cardId = r('id');
$act = r('act');
if ($act == 'add') {
    foreach ($fujian['name'] as $idx => $data) {
        if ($fujian['error'][$idx] == 0) {
            $id = uniqid() . $idx;

            $filename = iconv('GBk', 'UTF-8//IGNORE', $fujian['name'][$idx]);
            $filename = $fujian['name'][$idx];
            $houzhui = substr(strrchr($filename, '.'), 1);
            $newName = "/uploader/{$id}.{$houzhui}";

            $fileData[$id] = array(
                "name" => $filename,
                "bumen" => r('bumen'),
                "file" => $newName
            );
            rename($fujian['tmp_name'][$idx], $newName);

        }
    }


    $sql = "select * from calendar where id='{$cardId}'";
    $rs = DB::getOne($sql);
    $oldFujian = json_decode($rs['fujian'],true);
    if (!$oldFujian)
        $oldFujian = array();
    $dict = array();
    $dict['fujian'] = json_encode(array_merge($fileData, $oldFujian), true);
    $sql = DB::update('calendar', $dict, "id='{$cardId}'");
    DB::exe($sql);
    we(1);
}
else if($act=='del'){
    $fujianId = r('key');
    $sql = "select * from calendar where id='{$cardId}'";
    $rs = DB::getOne($sql);
    $oldFujian = json_decode($rs['fujian'], true);
    unset($oldFujian[$fujianId]);
    $dict = array();
    $dict['fujian'] = json_encode($oldFujian, true);
    $sql = DB::update('calendar', $dict, "id='{$cardId}'");
    DB::exe($sql);
    we(1);
}


?>