What are the differences between using str_replace and rawurlencode functions in PHP for handling file names?

When handling file names in PHP, it's important to properly encode special characters to ensure compatibility and prevent errors. The str_replace function can be used to replace specific characters with their encoded equivalents, while the rawurlencode function can encode the entire string. The main difference is that str_replace allows for more control over which characters are encoded, while rawurlencode encodes all characters that are not alphanumeric.

// Using str_replace to replace specific characters with their encoded equivalents
$filename = "my file.txt";
$encoded_filename = str_replace(" ", "%20", $filename);

// Using rawurlencode to encode the entire file name
$filename = "my file.txt";
$encoded_filename = rawurlencode($filename);