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
$key string "" Optional key to use - to prevent conflicts when simultaneous calls use same file name

Return

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

Location

include/file_functions.php lines 125 to 211

Definition

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

    if (
$key != "" && preg_match('/\W+/'$key) !== 0)
        {
        
// Block potential path traversal - allow only word characters.
        
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 $userref "_" $key generateUserFilenameUID($userref);
    
$tmp_dir get_temp_dir(false) . "/remote_files/" $tmp_uniq_path_id;
    if(!
is_dir($tmp_dir))
        {
        
mkdir($tmp_dir,0777,true);
        }
    
$tmp_file_path $tmp_dir"/" $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 30th January 2023 08:35 Europe/London time based on the source file dated 27th October 2022 16:50 Europe/London time.