Actions

API15

JInstallerHelper/downloadPackage

From Joomla! Documentation

< API15:JInstallerHelper
Revision as of 17:10, 22 March 2010 by Doxiki (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

[Edit Descripton]

Description:JInstallerHelper/downloadPackage

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';
        ini_set('track_errors', true);
 
        // Set user agent
        ini_set('user_agent', "Joomla! 1.5 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);
        }
 
        // Initialize 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);
 
        // Return the name of the downloaded package
        return basename($target);
}

[Edit See Also] SeeAlso:JInstallerHelper/downloadPackage

Examples

<CodeExamplesForm />