1

I'm a bit stuck with this. I have this bit of code which manages to get the filename of my file:

class AControllerA extends JControllerForm
{
    function save()
    {
        //Upload file
        jimport('joomla.filesystem.file');
        $jinput = JFactory::getApplication()->input;
        $store_form = $jinput->get('jform', null, 'array');
            $file = $store_form['img_url'];
        echo $file;
     }
}

*The file field has a name of jform[img_url];

However I cannot seem to get the 'tmp_name' for the file. Anyone know what I'm missing out? I'm a bit confused as to how jinput works...jrequest worked quite easily. Thanks!

models/forms/a.xml

<form enctype="multipart/form-data">
        <fieldset>
        <field
                        name="img_url"
                        type="file"
                        label=""
                        description=""
                        size="40"
                        class="inputbox"
                        default=""
                />
       </fieldset>
</form>
Moo33
  • 1,251
  • 3
  • 15
  • 27
  • I doubt that the enctype is copied to the HTML form. You need to add that to the form tag in your layout file. – inf3rno Mar 17 '17 at 20:06

3 Answers3

5

How about like this:

$files = $input->files->get('jform', null);
$filename = $files['img_url']['tmp_name'];
echo $filename;

Check out documentation for Retrieving file data using JInput

Marko D
  • 7,576
  • 2
  • 26
  • 38
  • Thanks for the reply. Unfortunately, it still doesn't echo anything. I've included my form code above, I'm not sure if it's because of the form? – Moo33 Mar 28 '13 at 08:10
  • i just saw that you double posted this from yesterday, and it's not nice that you didn't even respond to people who replied to you yesterday, but have just posted again. anyway, you can try dumping content of the `$files` and see what it contains – Marko D Mar 28 '13 at 08:21
  • Thanks for the reply again. I did reply but they were deleted. Also, I thought since the question is now different I created a new post. Sorry, just a bit stressed cause it doesn't seem to be working when I've tried almost everything. – Moo33 Mar 28 '13 at 08:30
  • did you try var_dump() of those variables? and is your php version less than 5.3? – Marko D Mar 28 '13 at 08:34
  • My PHP version is 5.4.10 and after dumping $files, it says NULL. I'm running MAMP on my mac if it makes any difference...though I doubt it does :/ It's weird though cause '$jinput->get' works while '$jinput->files->get' doesn't. – Moo33 Mar 28 '13 at 08:38
3

Supposing you are using JForm and the file input type, then you can access the file using this:

$files = $jinput->files->get('jform');
$file = $files['img_url']['tmp_name']

Also make sure your form has the enctype="multipart/form-data" set, otherwise it will not work.

Bakual
  • 2,731
  • 1
  • 13
  • 16
  • I tried echoing $files and $file but nothing seems to be showing up. $jinput = JFactory::getApplication()->input; $files = $jinput->files->get('jform', null); echo $files; $filename = $files['img_url']['tmp_name']; echo $filename; – Moo33 Mar 28 '13 at 08:21
  • 1
    Echo will probably not work since the page gets redirected after the save. Thus you never get to see the output. You would need a `die();` after the echo statements. Or you can use an extension like JDump (http://extensions.joomla.org/extensions/miscellaneous/development/1509) to dump some variables. – Bakual Mar 28 '13 at 09:00
  • Thanks for the answers! It echoes fine as I commented out the redirect. Only problem is I can't get '$jinput->files->get' to work while '$jinput->get' does. Is there any workaround without using jinput? Thanks! – Moo33 Mar 28 '13 at 10:26
  • I don't think so, except if you use the depreceated JRequest. It didn't work for me when the `enctype="multipart/form-data"` wasn't set, but it worked fine when I have set this part for the form. Also if you're using the JForm together with the _File_ FormField, it is accessed like shown in the answer. If you're using your own input field outside of the JForm class, then you can access it directly with the name of the field. You can try this. – Bakual Mar 28 '13 at 12:47
  • Thanks so much for the advice and replies. You rock. :) – Moo33 Mar 29 '13 at 04:01
2

In your model you should have sth like this

 public function getForm($data = array(), $loadData = false)
    {
        /**
         * Get the Form
         */
        $form = $this->loadForm('com_mycomponent.mycomponent', 'mycomponent',
                                        array('control' => false, 'load_data' => $loadData));
        if (empty($form)) {
            return false;
        }
        return $form;
    }

Note that $loaddata and 'control' is set to false, when 'control' is false you can get file parameters as according to the name specified in your xml i.e the output form is like:

<input name="name in xml file" type="file" />

If 'control' => 'jform'

<input name="jform[name in xml file]" type="file" />

$loaddata= false means you dont need to fetch any data from the database to the form.

in your view.html.php you should have sth like this

public function display($tpl = null)
    {
        $this->formData = $this->get('Form');
        $this->addToolbar();
        parent::display($tpl);
    }

Lets suppose I'll receive the requested file in "upload" method of "mycomponent" controller then it should have sth like this:

class MycomponentControllerMycomponent extends JControllerAdmin
{
public function upload()
    {
        //Retrieve file details from uploaded file, sent from upload form
        $file = JFactory::getApplication()->input->files->get('name in xml 
        **$tmp_name** = $file['tmp_name'];

    }
}

$tmp_name is Your required name

DrGeneral
  • 1,844
  • 1
  • 16
  • 22