Actions

API16

JStream/copy

From Joomla! Documentation

< API16:JStream
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

Copy a file from src to dest

[Edit Descripton]

Description:JStream/copy

Syntax

copy($src, $dest, $context=null, $use_prefix=true, $relative=false)
Parameter Name Default Value Description
$src
$dest
$context null
$use_prefix true
$relative false

Defined in

libraries/joomla/filesystem/stream.php

Importing

jimport( 'joomla.filesystem.stream' );

Source Body

function copy($src, $dest, $context=null, $use_prefix=true, $relative=false)
{
        $res = false;
        // Capture PHP errors
        $php_errormsg = '';
        $track_errors = ini_get('track_errors');
        ini_set('track_errors', true);
 
        $chmodDest = $this->_getFilename($dest, 'w', $use_prefix, $relative);
        $exists = file_exists($dest);
        $context_support = version_compare(PHP_VERSION, '5.3', '>='); // 5.3 provides context support
        if($exists && !$context_support)
        {
                // the file exists and there is no context support
                // this could cause a failure as we may need to overwrite the file
                // so we write our own copy function that will work with a stream
                // context; php 5.3 will fix this for us (yay!)
                // Note: since open processes the filename for us we won't worry about
                // calling _getFilename
                $res = $this->open($src);
                if($res) {
                        $reader = $this->_fh;
                        $res = $this->open($dest, 'w');
                        if($res)
                        {
                                $res = stream_copy_to_stream($reader, $this->_fh);
                                $tmperror = $php_errormsg; // save this in case fclose throws an error
                                @fclose($reader);
                                $php_errormsg = $tmperror; // restore after fclose
                        }
                        else
                        {
                                @fclose($reader); // close the reader off
                                $php_errormsg = JText::_('Failed to open writer') .': '. $this->getError();
                        }
                }
                else
                {
                        if(!$php_errormsg) {
                                $php_errormsg = JText::_('Failed to open reader') .': '. $this->getError();
                        }
                }
        }
        else
        {
                // since we're going to open the file directly we need to get the filename
                // we need to use the same prefix so force everything to write
                $src = $this->_getFilename($src, 'w', $use_prefix, $relative);
                $dest = $this->_getFilename($dest, 'w', $use_prefix, $relative);
                if($context_support && $context) { // use the provided context
                        $res = @copy($src, $dest, $context);
                } else if($context_support && $this->_context) { // use the objects context
                        $res = @copy($src, $dest, $this->_context);
                } else { // don't use any context
                        $res = @copy($src, $dest);
                }
        }
        if(!$res && $php_errormsg) {
                $this->setError($php_errormsg);
        } else {
                $this->chmod($chmodDest);
        }
 
        // restore error tracking to what it was before
        ini_set('track_errors',$track_errors);
        return $res;
}

[Edit See Also] SeeAlso:JStream/copy

Examples

<CodeExamplesForm />