How can one add new fonts to a PHP project without compromising update security?

When adding new fonts to a PHP project, it is important to ensure that the fonts are securely stored and accessed to prevent any compromise on update security. One way to achieve this is by storing the fonts in a separate directory outside of the web root and using PHP to dynamically load and serve the fonts when needed.

// Define the path to the fonts directory outside of the web root
$fontsDirectory = '/path/to/fonts/';

// Function to load and serve fonts securely
function loadFont($fontFile) {
    global $fontsDirectory;
    
    // Validate font file to prevent directory traversal attacks
    if (strpos($fontFile, '..') === false) {
        $fontPath = $fontsDirectory . $fontFile;
        
        // Serve the font file if it exists
        if (file_exists($fontPath)) {
            header('Content-Type: font/ttf');
            readfile($fontPath);
            exit;
        }
    }
    
    // Handle error if font file is not found
    header('HTTP/1.1 404 Not Found');
    exit;
}

// Example of loading a font file
loadFont('custom-font.ttf');