Skip to content

Instantly share code, notes, and snippets.

@sasivarnakumar
Created December 4, 2012 11:54
Show Gist options
  • Save sasivarnakumar/4203024 to your computer and use it in GitHub Desktop.
Save sasivarnakumar/4203024 to your computer and use it in GitHub Desktop.
Its very simple, I just thought of sharing the code I have just wrote to upload multiple files, using a Joomla based Framework.
/*
And my function in the controller to upload the file is:
*/
function uploadAttachments($id){
$files = JRequest::getVar('attachment_file', array(), 'files', 'array');
$params = JComponentHelper::getParams('com_mycomp');
$sizes = $files['size'];
$attachSize= $params->get('AttachmentMaxSize');
$attachSize = $attachSize * 1000;
for ($i = 0; $i < sizeof($sizes); $i++) {
if( $sizes[$i] > $attachSize){
$error=JText::_('Maximum upload size limit exceeded. Maximum upload size').' '.($attachSize!=0?$attachSize/1000:$attachSize).'kb';
}
}
if(!empty($error)){
$this->setError($error);
return false;
}
$user = &JFactory::getUser();
$app = &JFactory::getApplication();
if(!$id){
$this->setError(JText::_('You should save a valid order'));
return false;
}
$folder_base_path = $params->get('attachmentfolderpath');
$mode = (int)0755;
$index_file_src_path = JPATH_SITE.DS.'media'.DS.'index.html';
$index_file_dest_path = $folder_path.DS.'index.html';
//lets check if this base path exisists
if( !JFolder::exists($folder_base_path)){
$this->setError(JText::_('Please set the correct path to upload files in params'));
return false;
}
//if exists lets create products folder here
$products_folder_path = $folder_base_path.DS.'Files';
if( !JFolder::exists($products_folder_path)){
if(! ( JFolder::create($products_folder_path,$mode)
&& JFile::copy($index_file_src_path,$products_folder_path.DS.'index.html') ) ) {
$this->setError(JText::_('Error in creating directory'));
return false;
}
}
//Lets move on now to create specific folders and store the files
$folder_path = $products_folder_path.DS.$order_id;
//check if folder for the current product exists and
//if not a folder in name of 'prod_id' is created and index.html copied
if( ! JFolder::exists($folder_path)){
if(! ( JFolder::create($folder_path,$mode)
&& JFile::copy($index_file_src_path,$folder_path.DS.'index.html') ) ) {
$this->setError(JText::_('Error in creating directory'));
return false;
}
}
foreach ($files as $k=>$l) {
foreach ($l as $i=>$v) {
if (!array_key_exists($i, $attachmentFiles))
$attachmentFiles[$i] = array();
$attachmentFiles[$i][$k] = $v;
}
}
$maxNoOfFiles = $params->get('noOfAttachments',5);
$i=1;
foreach($attachmentFiles as $file){
// upload the file as it is without rename , resize as it is.
$file_src = $file['tmp_name'];
$filename = JFile::makeSafe($file['name']);
$save_path = $folder_path.DS.$filename;
// check weather the file type is allowed (or) File type filter
$allowed_file_types = array('jpg','jpeg','png','gif','pdf','tiff','psd','zip','bmp');
$fext = strtolower(JFile::getExt($filename));
if((in_array($fext, $allowed_file_types) == true) && $file["tmp_name"] ){
if(!JFile::upload($file_src, $save_path)) {
$this->setError(JText::_('Error in uploading product files'));
return false;
}
}
$i++;
if($maxNoOfFiles<$i){
break;
}
}
return true;
}
<!--
// In the view or layout I have put:
-->
<form action="<?php echo $action; ?>" method="post" id="adminform"
class="form-validate" name="adminForm" enctype="multipart/form-data">
<?php for ($i = 1; $i <= $this->params->get('noOfAttachments',5); $i++) { ?>
<div class="input" >
<input type="file" name="attachment_file[]" />
</div>
<div class="clr"></div>
<?php } ?>
</form>
Copy link

ghost commented Mar 12, 2016

how include a language file in muiltiple payments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment