Joomla backend component file upload for custom component

1.6k views Asked by At

I am new in Joomla! I am creating custom joomla(3.4.1) component, and i am making a good progress. I used almost all fields for one of the form for add/edit all works fine EXCEPT FILE UPLOAD.

Actually it saves value in field of my table, But i really don't know how/where to write Joomla! code for upload files.

i refer this link https://docs.joomla.org/Creating_a_file_uploader_in_your_component but i don't need any uploader at all,just want to upload manually, also find type="media" field, but it seems it is only allow for upload images.

Now what next step i should take in order to upload file in specific folder which is restricted for admin only.

I know one of the method MAY BE in controller to override save method and start uploading.

But is there any proper way to do it???

Thank you.

1

There are 1 answers

0
Hardik Raval On

Well,

I have figured this thing by this way!

I my controller file i wrote this code, will be back if get best way to do this.

<?php

defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.file');

class HelloWorldControllerHelloWorld extends JControllerForm{
    public function save($data = array(), $key = 'id')
    {
        $jinput = JFactory::getApplication()->input;
        $files = $jinput->files->get('jform');      

        $fileinfo =pathinfo($files['file_name']['name']);

        $ext = $fileinfo['extension'];
        $filename = time().".$ext";     

        $tmp_file = $files['file_name']['tmp_name'];
        $dest = "media/".JText::_('COM_APISLIDE')."/upload/".$filename;

        if (JFile::upload($tmp_file, $dest)) {
              // successed :)
        } else {              
             // failed :(
        }        
        parent::save();
    }
}