Example #1
0
 /**
  * Upload new file.
  *
  * @param   string  $file      The name of the file.
  * @param   string  $location  Location for the new file.
  *
  * @return   boolean  True if file uploaded successfully, false otherwise
  *
  * @since   3.2
  */
 public function uploadFile($file, $location)
 {
     jimport('joomla.filesystem.folder');
     if ($template = $this->getTemplate()) {
         $app = JFactory::getApplication();
         $client = JApplicationHelper::getClientInfo($template->client_id);
         $path = JPath::clean($client->path . '/templates/' . $template->element . '/');
         $fileName = JFile::makeSafe($file['name']);
         $err = null;
         JLoader::register('TemplateHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/template.php');
         if (!TemplateHelper::canUpload($file, $err)) {
             // Can't upload the file
             return false;
         }
         if (file_exists(JPath::clean($path . '/' . $location . '/' . $file['name']))) {
             $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
             return false;
         }
         if (!JFile::upload($file['tmp_name'], JPath::clean($path . '/' . $location . '/' . $fileName))) {
             $app->enqueueMessage(JText::_('COM_TEMPLATES_FILE_UPLOAD_ERROR'), 'error');
             return false;
         }
         $url = JPath::clean($location . '/' . $fileName);
         return $url;
     }
 }