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();
Keywords
Related Questions
- What best practices should beginners follow when writing PHP scripts to ensure compatibility with register_globals set to Off?
- How can error reporting be enabled in PHP to display error messages?
- How can strpos() and substr() be used to retain only characters before an underscore in a filename in PHP?