In a PHP file upload script, what are the best practices for converting a file path into a usable URL for storage in a database?

When storing file paths in a database, it's important to convert them into usable URLs for easy retrieval and display on a webpage. One common approach is to store the file path relative to the web server's document root and then construct the URL dynamically based on the server's domain. This ensures that the file paths can be easily converted into accessible URLs.

// Assuming $filePath contains the file path to be converted into a URL
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$filePathRelativeToRoot = str_replace($documentRoot, '', $filePath);
$url = 'http://' . $_SERVER['HTTP_HOST'] . $filePathRelativeToRoot;