Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

get_download_filename()

Description

Work out the filename to use when downloading the specified resource file with the given settings

Parameters

ColumnTypeDefaultDescription
$ref int Resource ID
$size string size code
$alternative int Alternative file ID
$ext string File extension

Return

string Filename to use

Location

include/resource_functions.php lines 7956 to 8057

Definition

 
function get_download_filename($ref,$size,$alternative,$ext)
    {
    
# Constructs a filename for download
    
global $original_filenames_when_downloading,$download_filenames_without_size,$download_id_only_with_size,
    
$download_filename_id_only,$download_filename_field,$prefix_resource_id_to_filename,$filename_field,
    
$prefix_filename_string$filename,$server_charset;
    
    
$filename = (($download_filenames_without_size || $size == "") ? "" "_" $size "") . ($alternative>"_" $alternative "") . "." $ext;
    
    if (
$original_filenames_when_downloading)
        {
        
# Use the original filename.
        
if ($alternative>0)
            {
            
# Fetch from the resource_alt_files alternatives table (this is an alternative file)
            
$origfile=get_alternative_file($ref,$alternative);
            
$origfile=$origfile["file_name"];
            }
        else
            {
            
# Fetch from field data or standard table   
            
$origfile=get_data_by_field($ref,$filename_field);  
            }
        if (
strlen($origfile)>0)
            {
            
# do an extra check to see if the original filename might have uppercase extension that can be preserved.   
            
$pathparts=pathinfo($origfile);
            if (isset(
$pathparts['extension'])){
                if (
strtolower($pathparts['extension'])==$ext){$ext=$pathparts['extension'];}   
            } 
            
            
# Use the original filename if one has been set.
            # Strip any path information (e.g. if the staticsync.php is used).
            # append preview size to base name if not the original
            
if($size != '' && !$download_filenames_without_size)
                {
                
$filename strip_extension(mb_basename($origfile),true) . '-' $size '.' $ext;
                }
            else
                {
                
$filename strip_extension(mb_basename($origfile),true) . '.' $ext;
                }
            }
        }

    elseif (
$download_filename_id_only)
        {
        if(!
hook('customdownloadidonly''', array($ref$ext$alternative)))
            {
            
$filename=$ref "." $ext;

            if(
$size != '' && $download_id_only_with_size)
                {
                
$filename $ref '-' $size '.' $ext;
                }            
            }
        }
    
    elseif (isset(
$download_filename_field))
        {
        
$newfilename=get_data_by_field($ref,$download_filename_field);
        if (
$newfilename)
            {
            
$filename trim(nl2br(strip_tags($newfilename)));
            if(
$size != "" && !$download_filenames_without_size)
                {
                
$filename strip_extension(mb_basename(substr($filename0200)),true) . '-' $size '.' $ext;
                }
            else
                {
                
$filename strip_extension(mb_basename(substr($filename0200)),true) . '.' $ext;
                }
            }
        }

    if(
$prefix_resource_id_to_filename)
        {
        
$filename $ref . (substr($filename,0,1) == "." "" '_') . $filename;
        }
    
    if(isset(
$prefix_filename_string) && trim($prefix_filename_string) != '')
        {
        
$filename $prefix_filename_string $filename;
        }

    
# Remove critical characters from filename
    
$altfilename=hook("downloadfilenamealt");
    if(!(
$altfilename)) $filename preg_replace('/:/''_'$filename);
    else 
$filename=$altfilename;

    
# Convert $filename to the charset used on the server.
    
if (!isset($server_charset)) {$to_charset 'UTF-8';}
    else
        {
        if (
$server_charset!="") {$to_charset $server_charset;}
        else {
$to_charset 'UTF-8';}
        }
    
$filename mb_convert_encoding($filename$to_charset'UTF-8');

    
hook("downloadfilename");
    return 
$filename;
    }

This article was last updated 24th November 2020 18:35 Europe/London time based on the source file dated 13th November 2020 16:50 Europe/London time.