Skip to content

Instantly share code, notes, and snippets.

@javimata
Created February 20, 2017 21:23
Show Gist options
  • Save javimata/5360417cc7d9190878a49f7b9e4f2730 to your computer and use it in GitHub Desktop.
Save javimata/5360417cc7d9190878a49f7b9e4f2730 to your computer and use it in GitHub Desktop.
//Support for file field: file_1
$input = JFactory::getApplication()->input;
$files = $input->files->get('jform');
if(!empty($files['file_1'])){
jimport('joomla.filesystem.file');
$file = $files['file_1'];
//Check if the server found any error.
$fileError = $file['error'];
$message = '';
if($fileError > 0 && $fileError != 4) {
switch ($fileError) {
case 1:
$message = JText::_( 'File size exceeds allowed by the server');
break;
case 2:
$message = JText::_( 'File size exceeds allowed by the html form');
break;
case 3:
$message = JText::_( 'Partial upload error');
break;
}
if($message != '') {
JError::raiseWarning(500, $message);
return false;
}
}
else if($fileError == 4){
if(isset($array['file_1_hidden'])){
$array['file_1'] = $array['file_1_hidden'];
}
}
else{
//Replace any special characters in the filename
$filename = explode('.', $file['name']);
$filename[0] = preg_replace("/[^A-Za-z0-9]/i", "-", $filename[0]);
//Add Timestamp MD5 to avoid overwriting
$filename = md5(time()) . '-' . implode('.',$filename);
$uploadPath = JPATH_ADMINISTRATOR . '/components/com_descargas/files/' . $filename;
$fileTemp = $file['tmp_name'];
if(!JFile::exists($uploadPath)){
if (!JFile::upload($fileTemp, $uploadPath)){
JError::raiseWarning(500, 'Error moving file');
return false;
}
}
$array['file_1'] = $filename;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment