What are the best practices for formatting file names with spaces in PHP URLs?

When dealing with file names that contain spaces in PHP URLs, it is best practice to replace the spaces with URL-encoded characters such as %20. This ensures that the file names are properly interpreted by the server and prevents any issues with handling spaces in URLs.

// Replace spaces in file name with %20
$filename = str_replace(' ', '%20', $filename);

// Use the modified file name in the URL
$url = "http://example.com/files/" . $filename;

// Redirect to the URL
header("Location: $url");
exit();