<?php
/* =================================================================================
 * License: GPL-2.0 license
 * Author: 众产®  https://ciy.cn/code
 * Version: 0.6.10
 * =================================================================================*/

namespace ciy;

class upload {
    public static function Fileext($filename) {
        $name = str_replace('\\', '/', $filename);
        $pos = strrpos($name, '/');
        $name = (false === $pos) ? $name : substr($name, $pos + 1);
        $extfile = strtolower(pathinfo($name, PATHINFO_EXTENSION)); // jpg
        $name = substr($name, 0, -strlen($extfile) - 1); //name去掉扩展名
        return array($name, $extfile);
    }
    public static function SaveUploadFile($path, $file, $rep = false) {
        if (!$rep) {
            if (file_exists(PATH_WEB . 'ud/' . $path))
                ciy_ouputJSON(errjson('文件已存在'));
        }
        $tpath = dirname(PATH_WEB . 'ud/' . $path);
        dirmake($tpath);
        $tstr = file_get_contents($file['tmp_name']);
        if (strpos($tstr, '<?php') !== false)
            ciy_ouputJSON(errjson('文件内容不合法'));
        move_uploaded_file($file['tmp_name'], PATH_WEB . 'ud/' . $path);
        return array('url' => '/' . $path);
    }
    public static function UploadError($err) {
        $size = 0;
        if (isset($_SERVER['CONTENT_LENGTH']))
            $size = toint($_SERVER['CONTENT_LENGTH']);
        if ($err == 1)
            return '上传参数出错:文件大小超出服务器限制 ' . ($size > 0 ? round($size / 1024 / 1024, 2) . 'MB' : '');
        if ($err == 2)
            return '上传参数出错:文件大小超出表单限制 ' . ($size > 0 ? round($size / 1024 / 1024, 2) . 'MB' : '');
        if ($err == 3)
            return '上传参数出错:文件部分上传';
        if ($err == 4)
            return '上传参数出错:没有文件上传';
        if ($err == 6)
            return '上传参数出错:找不到临时文件夹';
        if ($err == 7)
            return '上传参数出错:文件写入失败';
        return '上传参数未知错误:' . $err;
    }
}
