How can PHP handle file names with spaces in URLs?

When handling file names with spaces in URLs, PHP can use the urlencode() function to encode the file name before including it in the URL. This will convert any spaces in the file name to %20, which is the URL encoding for space. When the URL is accessed, PHP can then use urldecode() to decode the file name back to its original form.

// Encode the file name with spaces
$encodedFileName = urlencode("file name with spaces.txt");

// Include the encoded file name in the URL
$url = "http://example.com/files/" . $encodedFileName;

// When accessing the URL, decode the file name back to its original form
$decodedFileName = urldecode($encodedFileName);