Why does the superglobal $_FILES end up empty in my component?

664 views
Skip to first unread message

Valentin Despa

unread,
Apr 15, 2013, 7:07:25 AM4/15/13
to joomla-de...@googlegroups.com
Hi all,

I am really puzzled about this one because I don't quite understand what is going on.

I have an <input type="file" ...> and a jQuery script which sends a POST request to the server with the "attachment" that was selected. So jQuery is doing it's job.

The thing works great in plain PHP, $_FILES get's populated and it's good.

But in my component, when I print $_FILES, I get an empty array (and I can't figure out why).

So where does Joomla alter $_FILES?

Anybody had similar problems?


Kind regards,

Valentin Despa


Valentin Despa

unread,
Apr 15, 2013, 7:08:30 AM4/15/13
to joomla-de...@googlegroups.com
Forgot to add that I am talking about Joomla 3.0.3 and the thing is in front-end.

Aymeric Dourthe

unread,
Apr 15, 2013, 7:13:31 AM4/15/13
to joomla-de...@googlegroups.com
Hi,

if i remember well in Joomla 3 you have tu user JInput (instead of
JRequest instead of $_FILES).

Maybe something like that :

$jinput = JFactory::getApplication()->input;
$input->files->get('yourform');

Best regards,
Aymeric Dourthe
French web engineer

Le 15/04/2013 13:08, Valentin Despa a �crit :
> Forgot to add that I am talking about Joomla 3.0.3 and the thing is in
> front-end.
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com.
> To post to this group, send an email to
> joomla-de...@googlegroups.com.
> Visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Radek Suski

unread,
Apr 15, 2013, 7:17:45 AM4/15/13
to joomla-de...@googlegroups.com, joomla-de...@googlegroups.com
I don't think it is possible to send a file via post with plain jQuery.
You cannot send a file via Ajax "just" like this.

Sent from my iPad

Valentin Despa

unread,
Apr 15, 2013, 7:55:26 AM4/15/13
to joomla-de...@googlegroups.com
I don't worry about the jQuery part, because I know it's working just fine (as I said, in plain PHP outside Joomla it works, $_FILES is populated).

Anyway, I've made it even more simple.

<form action="index.php?option=com_mycomponent&controller=upload&format=raw" enctype="multipart/form-data" method="post">
        <input type="file" name="jform1[test][]" />
        <input type="submit" value="submit" />
</form>

class MyComponentControllersUpload extends JControllerBase
{
    public function execute()
    {
        var_dump($_FILES); // empty

       
        $jinput = JFactory::getApplication()->input;

        $files = $jinput->files->get('jform1');
       
        var_dump($files); // null
     }
}


Aymeric Dourthe

unread,
Apr 15, 2013, 8:00:33 AM4/15/13
to joomla-de...@googlegroups.com
If you append some other input can you access it in your execute function ?



Best regards,
Aymeric Dourthe
French web engineer

Valentin Despa

unread,
Apr 15, 2013, 8:07:13 AM4/15/13
to joomla-de...@googlegroups.com
This is really a weird thing now, so thanks to the idea to check out:


<form action="index.php?option=com_mycomponent&controller=upload&format=raw" enctype="multipart/form-data" method="post">
        <input type="file" name="jform1[test][]" />
        <input type="file" name="jform1[test][]" />
        <input type="text" name="filename" value="x" />

        <input type="submit" value="submit" />
</form>

So adding a <input type="text" /> makes the $_FILES variable work again (as expected).

Now how about that...

Radek Suski

unread,
Apr 15, 2013, 8:12:14 AM4/15/13
to joomla-de...@googlegroups.com
And  would actually.
How can you say that the jQuery part is working fine when you're not receiving data.
And you are not receiving because I doubt you can sent files with jQuery. Last time I was checking it wasn't possible.
I doubt that within few weeks it changes so much

Sent from my iPad

Aymeric Dourthe

unread,
Apr 15, 2013, 8:19:28 AM4/15/13
to joomla-de...@googlegroups.com
Strange behavior indeed.

Buti think  you can upload file with html5 file api.
Then you can use jQuery to send asynchronously your data.



Best regards,
Aymeric Dourthe
French web engineer

Valentin Despa

unread,
Apr 15, 2013, 8:38:35 AM4/15/13
to joomla-de...@googlegroups.com
@Radek - The jQuery part is working well, because I am receiving data in plain PHP (outside Joomla) as well as Joomla 2.5. Of course it's not plain jQuery but it does not matter as the request looks just fine. But I understand your concern.

Anyway, I've managed to reproduce this with a basic form (see pastes above). jQuery or not, $_FILES is still empty when no other data is sent along.

Don't get me wrong, but the title of my question is not if jQuery can or cannot post files, it's about why Joomla 3 is returning an empty $_FILES variable when it should not.

Thanks for trying to help.

Kind regards,

Valentin Despa

Radek Suski

unread,
Apr 15, 2013, 8:50:02 AM4/15/13
to joomla-de...@googlegroups.com
So please Post the jQuery code you are using to send the data.
I'm also not sure why you are trying to access (assumption) the $_files array directly instead of using the input class for it.

One thing I know for sure is that joomla isn't modifying it.



Sent from my iPad
--

Valentin Despa

unread,
Apr 15, 2013, 8:59:36 AM4/15/13
to joomla-de...@googlegroups.com
The jQuery script used can be found here:

https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin

Of course I've tried to get them with JInput, but JInputFiles also uses $_FILES, so for debugging it makes little sense to complicate things with JInput.

I am not so sure that Joomla is not modifying it, at least on my localhost, it seems that it does, but I don't want to get so excited about this, need to check on other installations as well.

If you have a moment, maybe you can take a look over the simple form I've pasted above (the one just with type="file"). There is no jQuery there. Plain html :)

Thanks,

Valentin Despa

Radek Suski

unread,
Apr 15, 2013, 2:44:12 PM4/15/13
to joomla-de...@googlegroups.com
We have implemented something similar in SobiPro, just with another jQuery plugin, and it works perfectly fine. 
Take a look: http://sobi-pro.net/entry/add << sorry but it is a weak Virtual Machine so it can be a bit slow.

Therefore I would rather suppose that it is the Ajax problem 

Nils Rückmann

unread,
Apr 15, 2013, 4:15:47 PM4/15/13
to joomla-de...@googlegroups.com
Hi Valentin, can you verify that the browser is really uploading the file (firebug etc.) ? Just to be sure, that there is no crazy JS which is cleaning your form before submit ?

Fedir

unread,
Apr 16, 2013, 8:32:26 AM4/16/13
to joomla-de...@googlegroups.com
if var_dump($_FILES) is empty then no files on server -> if no files on server, then they have not been sent -> if  they have not been sent, then it client side problem, in you case it some jQuery plugin ;)

if it work on clean PHP script but not in Joomla, then looks like you code for Joomla looks not the same ;)

have a questions:
  how much other script you use in the Joomla? (here can be cause of you problem)
  how and how much times jQuery.js conected?
  no "console errors"?

Понеділок, 15 квітня 2013 р. 15:38:35 UTC+3 користувач Valentin Despa написав:
Reply all
Reply to author
Forward
0 new messages