Skip to content

Instantly share code, notes, and snippets.

@Llewellynvdm
Last active February 3, 2022 16:46
Show Gist options
  • Save Llewellynvdm/49af4d28fbad098b3d3f23b1137d0cce to your computer and use it in GitHub Desktop.
Save Llewellynvdm/49af4d28fbad098b3d3f23b1137d0cce to your computer and use it in GitHub Desktop.
Upload Script
// see if we have a banner
<?php if (isset($this->item->banner) && [[[Component]]]Helper::checkString($this->item->banner)) : ?>
jQuery('<img src="<?php echo $this->item->banner; ?>" class="your-class-banner" alt="banner">').insertAfter(jQuery('#jform_banner').closest('.control-group'));
<?php endif; ?>
/**
* Works out an installation package from a HTTP upload.
*
* @return package definition or false on failure.
*/
protected function myUploadFunction($input)
{
// get the array of values
$userfile = $input->files->get('jform', null, 'array');
if (is_array($userfile))
{
$userfile = array_values($userfile)[0];
}
// Make sure that file uploads are enabled in php.
if (!(bool) ini_get('file_uploads'))
{
JError::raiseWarning('', JText::_('Uploads are not enabled in php.'));
return false;
}
// If there is no uploaded file
if (!is_array($userfile))
{
return false;
}
// Is the PHP tmp directory missing?
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_NO_TMP_DIR))
{
JError::raiseWarning(
'',
JText::_('Error')
);
return false;
}
// Is the max upload size too small in php.ini?
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_INI_SIZE))
{
JError::raiseWarning(
'',
JText::_('File to large')
);
return false;
}
// Check if there was a different problem uploading the file.
if ($userfile['error'] || $userfile['size'] < 1)
{
return false;
}
// Get classes
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
// Clean the name
$userfile['name'] = JPath::clean($userfile['name']);
// Build the appropriate paths.
$config = JFactory::getConfig();
$tmp_dest = $config->get('tmp_path') . '/' . $userfile['name'];
$tmp_src = $userfile['tmp_name'];
// Move uploaded file.
$p_file = JFile::upload($tmp_src, $tmp_dest, false, true);
// Was the package uploaded?
if (!$p_file)
{
JError::raiseWarning('', JText::_('Upload failed!'));
return false;
}
// check that this is a valid file
$package = $this->checkUpload($userfile, $tmp_dest);
return $package;
}
/**
* Check a file and verifies it as a allowed file format file
*
* @param string $archivename The uploaded package filename or import directory
*
* @return array of elements
*
*/
protected function checkUpload($archive, $tmp_dest)
{
// get file format
$fileFormat = strtolower(pathinfo($archive['name'], PATHINFO_EXTENSION));
// get fileFormat key
$allowedFormats = array('jpg','jpeg','png','gif');
// check the extension
if (!in_array($fileFormat, $allowedFormats))
{
// Cleanup the import files
$this->removeFile($tmp_dest);
$this->errorMessage = JText::_('Invalid file format.');
return false;
}
// set directory
$archive['full_path'] = $tmp_dest;
// set format
$archive['format'] = $fileFormat;
return $archive;
}
/**
* Clean up temporary uploaded file
*
* @param string $package Name of the uploaded file
*
* @return boolean True on success
*
*/
protected function removeFile($package)
{
// Is the package file a valid file?
if (is_file($package))
{
JFile::delete($package);
}
elseif (is_file(JPath::clean($package)))
{
// It might also be just a base filename
JFile::delete(JPath::clean($package));
}
}
// upload the image
if ($imageArray = $this->myUploadFunction($input))
{
// store the banner path to the database
$data['banner'] = '/images/'. $imageArray['name'];
// move the file into place
$final_dest = JPATH_ROOT . $data['banner'];
JFile::move($imageArray['full_path'], $final_dest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment