How does the use of urlencode differ when used in URLs versus file downloads in PHP?
When using urlencode in URLs in PHP, it is used to encode special characters in the URL to ensure proper transmission and interpretation by the server. However, when using urlencode for file downloads in PHP, it is important to decode the URL-encoded filename before serving the file to ensure the correct filename is displayed to the user.
// URL encoding for URLs
$url = 'https://example.com/page.php?param=' . urlencode($value);
// URL decoding for file downloads
$filename = urldecode($_GET['filename']);
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);