How can PHP developers avoid converting spaces to %20 in file paths when using functions like str_replace?

When using functions like str_replace to manipulate file paths in PHP, developers can avoid converting spaces to %20 by using the rawurlencode function instead. This function will encode spaces as %20 without affecting other characters in the path.

// Example code snippet to avoid converting spaces to %20 in file paths
$file_path = "/path/to/my file.txt";
$encoded_path = rawurlencode($file_path);

echo $encoded_path;