Actions

API16

JInstallerHelper/downloadPackage

From Joomla! Documentation

< API16:JInstallerHelper
Revision as of 22:01, 13 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Downloads a package

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

downloadPackage($url, $target=false)
Parameter Name Default Value Description
$url URL of file to download
$target false Download target filename [optional]

Returns

mixed Path to downloaded package or boolean false on failure

Defined in

libraries/joomla/installer/helper.php

Importing

jimport( 'joomla.installer.helper' );

Source Body

function downloadPackage($url, $target = false)
{
        $config = &JFactory::getConfig();
 
        // Capture PHP errors
        $php_errormsg = 'Error Unknown';
        $track_errors = ini_get('track_errors');
        ini_set('track_errors', true);
 
        // Set user agent
        jimport('joomla.version');
        $version = new JVersion();
        ini_set('user_agent', $version->getUserAgent('Installer'));
 
        // Open the remote server socket for reading
        $inputHandle = @ fopen($url, "r");
        $error = strstr($php_errormsg,'failed to open stream:');
        if (!$inputHandle)
        {
                JError::raiseWarning(42, JText::_('SERVER_CONNECT_FAILED').', '.$error);
                return false;
        }
 
        $meta_data = stream_get_meta_data($inputHandle);
        foreach ($meta_data['wrapper_data'] as $wrapper_data)
        {
                if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition")
                {
                        $contentfilename = explode ("\"", $wrapper_data);
                        $target = $contentfilename[1];
                }
        }
 
        // Set the target path if not given
        if (!$target) {
                $target = $config->getValue('config.tmp_path').DS.JInstallerHelper::getFilenameFromURL($url);
        }
        else {
                $target = $config->getValue('config.tmp_path').DS.basename($target);
        }
 
        // Initialise contents buffer
        $contents = null;
 
        while (!feof($inputHandle))
        {
                $contents .= fread($inputHandle, 4096);
                if ($contents == false)
                {
                        JError::raiseWarning(44, 'Failed reading network resource: '.$php_errormsg);
                        return false;
                }
        }
 
        // Write buffer to file
        JFile::write($target, $contents);
 
        // Close file pointer resource
        fclose($inputHandle);
 
        // restore error tracking to what it was before
        ini_set('track_errors',$track_errors);
 
        // Return the name of the downloaded package
        return basename($target);
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />