Back to File class

Method upload

public static bool
upload
(mixed $src, mixed $dest, mixed $useStreams = false, mixed $allowUnsafe = false, mixed $safeFileOptions = array())
Moves an uploaded file to a destination folder
Parameters
  • string $src The name of the php (temporary) uploaded file
  • string $dest The path (including filename) to move the uploaded file to
  • bool $useStreams True to use streams
  • bool $allowUnsafe Allow the upload of unsafe files
  • array $safeFileOptions Options to InputFilter::isSafeFile
Returns
  • bool True on success
Since
  • 1.7.0
Class: File
Project: Joomla

Method upload - Source code

/**
 * Moves an uploaded file to a destination folder
 *
 * @param   string   $src              The name of the php (temporary) uploaded file
 * @param   string   $dest             The path (including filename) to move the uploaded file to
 * @param   boolean  $useStreams       True to use streams
 * @param   boolean  $allowUnsafe      Allow the upload of unsafe files
 * @param   array    $safeFileOptions  Options to InputFilter::isSafeFile
 *
 * @return  boolean  True on success
 *
 * @since   1.7.0
 */
public static function upload($src, $dest, $useStreams = false, $allowUnsafe = false, $safeFileOptions = array())
{
    if (!$allowUnsafe) {
        $descriptor = array('tmp_name' => $src, 'name' => basename($dest), 'type' => '', 'error' => '', 'size' => '');
        $isSafe = InputFilter::isSafeFile($descriptor, $safeFileOptions);
        if (!$isSafe) {
            Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR03', $dest), Log::WARNING, 'jerror');
            return false;
        }
    }
    // Ensure that the path is valid and clean
    $dest = Path::clean($dest);
    // Create the destination directory if it does not exist
    $baseDir = \dirname($dest);
    if (!file_exists($baseDir)) {
        Folder::create($baseDir);
    }
    if ($useStreams) {
        $stream = Factory::getStream();
        if (!$stream->upload($src, $dest)) {
            Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_GENERIC', __METHOD__, $stream->getError()), Log::WARNING, 'jerror');
            return false;
        }
        return true;
    } else {
        $FTPOptions = ClientHelper::getCredentials('ftp');
        $ret = false;
        if ($FTPOptions['enabled'] == 1) {
            // Connect the FTP client
            $ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
            // Translate path for the FTP account
            $dest = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
            // Copy the file to the destination directory
            if (is_uploaded_file($src) && $ftp->store($src, $dest)) {
                self::invalidateFileCache($src);
                unlink($src);
                $ret = true;
            } else {
                Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
            }
        } else {
            self::invalidateFileCache($src);
            if (is_writable($baseDir) && move_uploaded_file($src, $dest)) {
                // Short circuit to prevent file permission errors
                if (Path::setPermissions($dest)) {
                    $ret = true;
                } else {
                    Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), Log::WARNING, 'jerror');
                }
            } else {
                Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
            }
        }
        self::invalidateFileCache($dest);
        return $ret;
    }
}