Collections functions
General functions
Render functions
Theme permission functions
Resource functions

temp_local_download_remote_file()

Description

Download remote file to the temp filestore location.

Parameters

ColumnTypeDefaultDescription
$url string Source URL

Return

string|bool Returns the new temp filestore location or false otherwise.

Location

include/file_functions.php lines 124 to 202

Definition

 
function temp_local_download_remote_file(string $url)
    {
    
$userref $GLOBALS['userref'] ?? 0;
    if(
$userref === 0)
        {
        return 
false;
        }

    
$url trim($url);
    
$url_original $url;
    
// Remove query string from URL
    
$url explode('?'$url);
    
$url reset($url);
    
    
$path_parts pathinfo(basename($url));
    
$filename safe_file_name($path_parts['filename'] ?? '');
    
$extension $path_parts['extension'] ?? '';
    
$filename .= ($extension !== '' ".{$extension}'');

    if(
strpos($filename,".") == false && filter_var($url_originalFILTER_VALIDATE_URL))
        {
        
// $filename not valid, try and get from HTTP header
        
$headers get_headers($url_original,true);
        foreach(
$headers as $header=>$headervalue)
            {
            if(
strtolower($header) == "content-disposition")
                {
                
// Check for double quotes first (e.g. attachment; filename="O'Malley's Bar.pdf")
                
if(preg_match('/.*filename=[\"]([^\"]+)/'$headervalue$matches))
                    {
                    
$filename $matches[1];
                    }
                
// Check for single quotes (e.g. attachment; filename='Space Travel.jpg')
                
elseif(preg_match('/.*filename=[\']([^\']+)/'$headervalue$matches))
                    {
                    
$filename $matches[1];
                    }
                
// Get file name up to first space
                
else if(preg_match("/.*filename=([^ ]+)/"$headervalue$matches))
                    {
                    
$filename $matches[1];
                    }
                }
            }
        }
    
// Get temp location
    
$tmp_uniq_path_id sprintf('remote_files/%s_%s'$userrefgenerateUserFilenameUID($userref));
    
$tmp_file_path sprintf('%s/%s',
        
get_temp_dir(false$tmp_uniq_path_id),
        
$filename);

    if(
$tmp_file_path == $url)
        {
        
// Already downloaded earlier by API call 
        
return $tmp_file_path;
        }

    
// Download the file
    
$GLOBALS['use_error_exception'] = true;
    try
        {
        if(
copy($url_original$tmp_file_path))
            {
            return 
$tmp_file_path;
            }
        }
    catch(
Throwable $t)
        {
        
debug(sprintf(
            
'Failed to download remote file from "%s" to temp location "%s". Reason: %s',
            
$url_original,
            
$tmp_file_path,
            
$t->getMessage()
        ));
        }
    unset(
$GLOBALS['use_error_exception']);

    return 
false;
    }

This article was last updated 26th September 2022 13:35 Europe/London time based on the source file dated 21st September 2022 09:25 Europe/London time.