修改typecho上传地址URL

教程 · 2024-07-05
修改typecho上传地址URL

原文件上传路径是usr/uploads/年/月/文件名
看起来太长了,修改为usr/uploads/文件名
修改方法
var/widget/upload.php中的uploadHandle方法替换为

public static function uploadHandle(array $file)
{
    if (empty($file['name'])) {
        return false;
    }

    $result = self::pluginHandle()->trigger($hasUploaded)->uploadHandle($file);
    if ($hasUploaded) {
        return $result;
    }

    $ext = self::getSafeName($file['name']);

    if (!self::checkFileType($ext)) {
        return false;
    }

    $path = Common::url(
        defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR,
        defined('__TYPECHO_UPLOAD_ROOT_DIR__') ? __TYPECHO_UPLOAD_ROOT_DIR__ : __TYPECHO_ROOT_DIR__
    );

    //创建上传目录
    if (!is_dir($path)) {
        if (!self::makeUploadDir($path)) {
            return false;
        }
    }

    //获取文件名
    $fileName = sprintf('%u', crc32(uniqid())) . '.' . $ext;
    $path = $path . '/' . $fileName;

    if (isset($file['tmp_name'])) {
        //移动上传文件
        if (!@move_uploaded_file($file['tmp_name'], $path)) {
            return false;
        }
    } elseif (isset($file['bytes'])) {
        //直接写入文件
        if (!file_put_contents($path, $file['bytes'])) {
            return false;
        }
    } elseif (isset($file['bits'])) {
        //直接写入文件
        if (!file_put_contents($path, $file['bits'])) {
            return false;
        }
    } else {
        return false;
    }

    if (!isset($file['size'])) {
        $file['size'] = filesize($path);
    }

    //返回相对存储路径
    return [
        'name' => $file['name'],
        'path' => (defined('__TYPECHO_UPLOAD_DIR__') ? __TYPECHO_UPLOAD_DIR__ : self::UPLOAD_DIR)
            . '/' . $fileName,
        'size' => $file['size'],
        'type' => $ext,
        'mime' => Common::mimeContentType($path)
    ];
}

Theme Jasmine by Kent Liao 萌ICP备20240471号