Example #1
0
 /**
  * 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  $use_streams  True to use streams
  *
  * @return  boolean  True on success
  *
  * @since   1.0
  * @throws  FilesystemException
  */
 public static function upload($src, $dest, $use_streams = 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 ($use_streams) {
         $stream = Stream::getStream();
         if (!$stream->upload($src, $dest)) {
             throw new FilesystemException(__METHOD__ . ': ' . $stream->getError());
         }
         return true;
     } else {
         if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
             // Short circuit to prevent file permission errors
             if (Path::setPermissions($dest)) {
                 return true;
             } else {
                 throw new FilesystemException(__METHOD__ . ': Failed to change file permissions.');
             }
         } else {
             throw new FilesystemException(__METHOD__ . ': Failed to move file.');
         }
         return false;
     }
 }
 /**
  * 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  $use_streams  True to use streams
  *
  * @return  boolean  True on success
  *
  * @since   11.1
  */
 public static function upload($src, $dest, $use_streams = 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 ($use_streams) {
         $stream = Factory::getStream();
         if (!$stream->upload($src, $dest)) {
             Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $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 = ClientFtp::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)) {
                 unlink($src);
                 $ret = true;
             } else {
                 Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'), Log::WARNING, 'jerror');
             }
         } else {
             if (is_writeable($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::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'), Log::WARNING, 'jerror');
             }
         }
         return $ret;
     }
 }