theme_file_link() double-encodes URLs, which breaks links to files that contain encoded characters.

theme_file_link() calls file_create_url(), which will usually call DrupalPublicStreamWrapper::getExternalUrl() which calls drupal_encode_path(). It then passes the result to l() which calls url() which, with clean URLs, also calls drupal_encode_path().

To replicate this issue, upload a file that has a space in its name. Determine the file's fid number. Then run the following code

$fid = 1; // Whatever the file was saved as
$file = file_load($fid);
print theme('file_link', array('file' => $file));

// And to show that it isn't due to a theme override:
print theme_file_link(array('file' => $file, 'icon_directory' => NULL));

Both of the links displayed will be encoded incorrectly. Spaces, for example will appear as %2520.

Comments

claw created an issue. See original summary.

vimalgoradiya’s picture

I am trying to replicate issue by uploading file name "Screenshot from 2015-09-07 14:24:22.png". I found on mouse over both links it displays same as its file name but on click of that link the behaviour is something like "Screenshot from 2015-09-07 14%3A24%3A22.png". In my case space would consider properly but colon may replace with special character %3A.

claw’s picture

Browsers seem to display an interpretation of the link. Check the page source - it's the only authority that matters. :-)

Version: 7.39 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.