API16

JFile/copy: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Copies a file <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki> </span> {{Descript...
 
m preparing for archive only
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
Copies a file
Copies a file


<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JFile/copy|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JFile/copy}}
 
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 100: Line 98:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JFile/copy|Edit See Also]]<nowiki>]</nowiki>
<! removed transcluded page call, red link never existed >
</span>
{{SeeAlso:JFile/copy}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=copy
  category=copy
  category=JFile
  category=JFile
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API16]]

Latest revision as of 01:39, 25 March 2017

Description

Copies a file


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

Syntax

copy($src, $dest, $path=null, $use_streams=false)
Parameter Name Default Value Description
$src $src The path to the source file
$dest $dest The path to the destination file
$path null $path An optional base path to prefix to the file names
$use_streams false

Returns

boolean True on success

Defined in

libraries/joomla/filesystem/file.php

Importing

jimport( 'joomla.filesystem.file' );

Source Body

function copy($src, $dest, $path = null, $use_streams=false)
{
        // Prepend a base path if it exists
        if ($path) {
                $src = JPath::clean($path.DS.$src);
                $dest = JPath::clean($path.DS.$dest);
        }

        //Check src path
        if (!is_readable($src)) {
                JError::raiseWarning(21, 'JFile::copy: ' . JText::_('Cannot find or read file') . ": '$src'");
                return false;
        }

        if($use_streams) {
                $stream =& JFactory::getStream();
                if(!$stream->copy($src, $dest)) {
                        JError::raiseWarning(21, 'JFile::copy('. $src .', '. $dest .'): '. $stream->getError());
                        return false;
                }
                return true;
        } else {
                // Initialise variables.
                jimport('joomla.client.helper');
                $FTPOptions = JClientHelper::getCredentials('ftp');

        if ($FTPOptions['enabled'] == 1) {
                // Connect the FTP client
                jimport('joomla.client.ftp');
                $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);

                // If the parent folder doesn't exist we must create it
                if (!file_exists(dirname($dest))) {
                        jimport('joomla.filesystem.folder');
                        JFolder::create(dirname($dest));
                }

                //Translate the destination path for the FTP account
                $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
                if (!$ftp->store($src, $dest)) {
                        // FTP connector throws an error
                        return false;
                }
                $ret = true;
        } else {
                if (!@ copy($src, $dest)) {
                        JError::raiseWarning(21, JText::_('COPY_FAILED'));
                        return false;
                }
                $ret = true;
        }
        return $ret;
}
}


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

Examples

Code Examples