What best practices should be followed when storing file paths in PHP for local use only?

When storing file paths in PHP for local use only, it is important to use absolute paths to ensure that the files can be accessed consistently regardless of the current working directory. Additionally, it is recommended to define a base path constant to easily reference the root directory of the project. Finally, sanitize user input to prevent directory traversal attacks.

<?php

// Define base path constant
define('BASE_PATH', __DIR__);

// Store file path using absolute path
$file_path = BASE_PATH . '/path/to/file.txt';

// Sanitize user input
$user_input = $_POST['input'];
$sanitized_input = realpath(BASE_PATH . '/' . $user_input);

// Use the sanitized input in file operations
if (strpos($sanitized_input, BASE_PATH) === 0) {
    // File path is within the project directory
    // Proceed with file operations
} else {
    // Invalid file path
    echo 'Invalid file path';
}

?>