Пример #1
0
function writeFile($urlAbs, $txtCnt)
{
    $extName = getExtName($urlAbs);
    switch ($extName) {
        case 'js':
            $comment = ["//", ""];
            break;
        case 'html':
            $comment = ["<!--", "-->"];
            break;
        case 'css':
            $comment = ["/*", "*/"];
            break;
        default:
            $comment = ["", ""];
            break;
    }
    $txtCnt = $comment[0] . "Written by PROCESS.PHP at the time of " . Date("Y-m-d H:i:s") . $comment[1] . PHP_EOL . $txtCnt;
    $bool = @file_put_contents(WEBROOT_AT_DISK . $urlAbs, $txtCnt);
    if ($bool !== false) {
        return;
    }
    $aDirs = explode('/', $urlAbs);
    array_shift($aDirs);
    $dirCur = WEBROOT_AT_DISK;
    for ($cntDirs = 0; $cntDirs < count($aDirs) - 1; $cntDirs++) {
        $dirCur .= '/' . $aDirs[$cntDirs];
        if (!is_dir($dirCur)) {
            mkdir($dirCur);
        }
    }
    file_put_contents(WEBROOT_AT_DISK . $urlAbs, $txtCnt);
}
Пример #2
0
function uploadFile($path = "uploads", $allowExt = array("gif", "jpeg", "png", "jpg", "wbmp", "blob"), $maxSize = 2097152000, $imgFlag = true)
{
    if (!file_exists($path)) {
        mkdir($path);
    }
    $i = 0;
    $files = buildInfo();
    if (!($files && is_array($files))) {
        return;
    }
    foreach ($files as $file) {
        if ($file['error'] == UPLOAD_ERR_OK) {
            $ext = getExtName($file['name']);
            //检测文件的扩展名
            if (!in_array($ext, $allowExt)) {
                exit("非文件类型");
            }
            //校验是否是一个真正的图片类型
            if ($imgFlag) {
                if (!getimagesize($file['tmp_name'])) {
                    exit("不是真正的图片类型");
                }
            }
            //上传文件的大小
            if ($file['size'] > $maxSize) {
                exit("上传文件过大");
            }
            if (!is_uploaded_file($file['tmp_name'])) {
                exit("不是通过HTTP POST方式上传上来的");
            }
            $filename = getUniqueName() . "." . $ext;
            $destination = $path . "/" . $filename;
            if (move_uploaded_file($file['tmp_name'], $destination)) {
                $file['name'] = $filename;
                unset($file['tmp_name'], $file['size'], $file['type']);
                $uploadedFiles[$i] = $file;
                $i++;
            }
        } else {
            switch ($file['error']) {
                case 1:
                    $mes = "超过了配置文件上传文件的大小";
                    //UPLOAD_ERR_INI_SIZE
                    break;
                case 2:
                    $mes = "超过了表单设置上传文件的大小";
                    //UPLOAD_ERR_FORM_SIZE
                    break;
                case 3:
                    $mes = "文件部分被上传";
                    //UPLOAD_ERR_PARTIAL
                    break;
                case 4:
                    $mes = "没有文件被上传";
                    //UPLOAD_ERR_NO_FILE
                    break;
                case 6:
                    $mes = "没有找到临时目录";
                    //UPLOAD_ERR_NO_TMP_DIR
                    break;
                case 7:
                    $mes = "文件不可写";
                    //UPLOAD_ERR_CANT_WRITE;
                    break;
                case 8:
                    $mes = "由于PHP的扩展程序中断了文件上传";
                    //UPLOAD_ERR_EXTENSION
                    break;
            }
            echo $mes;
        }
    }
    return $uploadedFiles;
}