<?php
class exfileupload2 {
    var $tmp_path;
    var $filelist = array();
    function exfileupload2($tmp_path) {
        $this->tmp_path = $tmp_path;
    }
    function move() {
        foreach ($_FILES as $key => $value) {
            if ($_FILES[$key]['error'] == UPLOAD_ERR_INI_SIZE) {
                $this->filelist["$key"]['name'] = '';
                $this->filelist["$key"]['uploaded_name'] = '';
                $this->filelist["$key"]['uploaded_basename'] = '';
                $this->filelist["$key"]['error'] = "exfileupload2 ERROR : can not upload over " . ini_get('upload_max_filesize') . " Bytes";
                die($this->filelist["$key"]['error']);
            } else if (is_uploaded_file($_FILES[$key]['tmp_name'])) {
                $ext = $this->_get_ext($_FILES[$key]['name']);
                if (move_uploaded_file($_FILES[$key]['tmp_name'], $this->tmp_path . '/' . basename($_FILES[$key]['tmp_name'] . $ext))) {
                    $this->filelist["$key"]['name'] = $_FILES[$key]['name'];
                    $this->filelist["$key"]['type'] = $_FILES[$key]['type'];
                    $this->filelist["$key"]['size'] = $_FILES[$key]['size'];
                    $this->filelist["$key"]['ext'] = $ext;
                    $this->filelist["$key"]['uploaded_name'] = $this->tmp_path . '/' . basename($_FILES[$key]['tmp_name'] . $ext);
                    $this->filelist["$key"]['uploaded_basename'] = basename($_FILES[$key]['tmp_name'] . $ext);
                    chmod($this->tmp_path . '/' . basename($_FILES[$key]['tmp_name'] . $ext), 0666);
                } else {
                    die('[ ERROR: can not move' . $this->tmp_path . ' ]');
                }
            }
        }
        return $this->filelist;
    }
    function _get_ext($name) {
        if (preg_match('/jpeg/', $name)) {
            return '.jpg';
        } else if (preg_match('/jpg/', $name)) {
            return '.jpg';
        } else if (preg_match('/gif/', $name)) {
            return '.gif';
        } else if (preg_match('/png/', $name)) {
            return '.png';
        } else if (preg_match('/\.([a-zA-z0-9]+)$/', $name, $result)) {
            $ext = strtolower($result[1]);
            return ".{$ext}";
        }
    }
    function delete_tmp($dirpath = '') {
        if ($dirpath == '') {
            $dirpath = $this->tmp_path;
        }
        $deleted_list = array();
        $dir = dir($dirpath);
        while (($file = $dir->read()) !== FALSE) {
            if (preg_match('/^\./', $file)) {
                continue;
            } else {
                $filetime = filemtime("$dirpath/$file");
                $nowtime = time();
                $int_f = intval($filetime);
                $int_n = intval($nowtime);
                $sa = ($int_n - $int_f);
                if ($sa > (60 * 60 * 24)) {
                    array_push($deleted_list, $file);
                    if (!unlink("$dirpath/$file")) {
                        die("ファイル[" . $dirpath . "/" . $file . "]の削除に失敗しました");
                    }
                }
            }
        }
        return $deleted_list;
    }
    function dump($data) {
        mb_convert_variables('EUC', 'auto', $data);
        print "<pre>";
        print_r($data);
        print "</pre>";
    }
} ?>