You are here

function field_file_urlencode_path in FileField 6.3

Encode a file path in a way that is compatible with file_create_url().

This function should be used on the $file->filepath property before any call to file_create_url(). This ensures that the file directory path prefix is unmodified, but the actual path to the file will be properly URL encoded.

2 calls to field_file_urlencode_path()
theme_filefield_file in ./filefield_formatter.inc
Theme function for the 'generic' single file formatter.
theme_filefield_formatter_url_plain in ./filefield_formatter.inc
Theme function for the 'url_plain' formatter.

File

./field_file.inc, line 385
Common functionality for file handling CCK field modules.

Code

function field_file_urlencode_path($path) {

  // Encode the parts of the path to ensure URLs operate within href attributes.
  // Private file paths are urlencoded for us inside of file_create_url().
  if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
    $file_directory_path = file_directory_path();
    if (strpos($path, $file_directory_path . '/') === 0) {
      $path = trim(substr($path, strlen($file_directory_path)), '\\/');
    }
    $parts = explode('/', $path);
    foreach ($parts as $index => $part) {
      $parts[$index] = rawurlencode($part);
    }
    $path = implode('/', $parts);

    // Add the file directory path again (not encoded).
    $path = $file_directory_path . '/' . $path;
  }
  return $path;
}