donglv7097 2015-09-27 17:05
浏览 135

通过PHP传递字体文件

I have a website that is set up like this:

client request --> gate.php --> requested file

Every request the client sends goes to gate.php were it is parsed. Gate.php then includes the requested file from a restricted directory so that the client cannot access any file but gate.php.

Gate file:

<?php
$_uri = strtok($_SERVER["REQUEST_URI"],'?');
$_root = "<root>";
// Index //
switch($_uri) {
    case "/": $_uri = "<path>"; break;
    case "/css": $_uri = "<path>"; break;
    case "/js": $_uri = "<path>"; break;
    case "/font": $_uri = "<path>".strtok($_GET["p"],".")."/".$_GET["p"]; break;
    case "/ajax": $_uri = "<path>"; break;
    case "/signin": $_uri = "<path>"; break;
    case "/signup": $_uri = "<path>"; break;
    default:
        if(substr($_uri,0,8) == "/profile") { // profile
            $_uri = "<path>";
            $_page = substr($_uri,9);
        } else {
            header("HTTP/1.1 404");
            require_once($_root."<path>");
            die();
        }
}
!isset($_page) and isset($_GET["p"]) ? $_page = $_GET["p"] : 0;
// Mime //
$_path = explode(".",$_uri);
switch($_path[1]) {
    case "php": $_path[2] = "text/html"; break;
    case "css": $_path[2] = "text/css"; break;
    case "js": $_path[2] = "application/javascript"; break;
    case "xml": $_path[2] = "application/xml"; break;
    case "svg": $_path[2] = "application/xml+svg"; break;
    case "jpg": $_path[2] = "image/jpeg"; break;
    case "png": $_path[2] = "image/png"; break;
    case "otf": $_path[2] = "x-font/otf"; break;
    case "eot": $_path[2] = "x-font/eot"; break;
    case "ttf": $_path[2] = "x-font/ttf"; break;
    case "woff": $_path[2] = "x-font/woff"; break;
    default:
        header("HTTP/1.1 500");
        require_once($_root."<path>");
        die();
}
$_path[2] == "text/html" ? require_once($_root."<path>") : 0;
// File //
header("Content-Type: ".$_path[2]);
require_once($_root."/sys".$_uri);

?>

The problem is, when I pass a font file through the gate, the font file contains the text <? which PHP parses and returns an error.

Is there any way to escape the font file so that PHP does not parse it?

  • 写回答

1条回答 默认 最新

  • duanhuhong5255 2015-09-27 17:18
    关注

    You can only require files that can be interpreted by PHP. If you want to serve other kinds of files through your script, you have to output them by reading them.

    Something like:

    $file = 'myfontfile.ttf';
    
    if (file_exists($file)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: inline; filename="'.basename($file).'"');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端