Ajax file upload

I’ve been trying to get the scripts from here working, without much luck. So I tried downloading the script from the page but I get the same results. It’s not uploading files although the progress bar shows them as uploaded. Nothing is shown in Status Messages and there are no console errors.

If I use dev tools to change the display style of the submit button back to block and click it, it uploads one file, but that’s not how it’s supposed to work I’m thinking…

Hm that’s very strange… I just tested it on chromium with PHP’s built-in dev server (just from the project root) and it worked fine for me. Does the upload.php request show up in the network panel of the browser dev tools?

1 Like

Another thing I would check is the upload_max_filesize and post_max_size in your php.ini file isn’t a small value. I’ve gone through hundreds of projects of mine struggling to figure out why things aren’t uploading and then finally find out I forget to increase those.

2 Likes

Ah, strange indeed.

Yes, it does.

Done that, too. I got caught out with another script, nothing uploading and eventually found it was a large file, so I’ve been testing it with the smallest images I could find.

Mebbe I should try it online, but that probably won’t be till the weekend now…

Is that request carrying a payload?

I’m not sure. How do I tell? Does this answer?

This is a payload (Firefox). I don’t know how to check it on Chrome since I don’t use Chrome. But if that payload is empty, there’s something wrong.

I don’t see where to find that info in FF @spaceshiptrooper

Right-click and then go to Inspection Tool. Next click on Network. That should give you the tab.

Rather belatedly I thought to look in the error log, and lo and behold I found

PHP Notice:  Undefined index: fileselect in upload.php on line 23,
PHP Warning:  Invalid argument supplied for foreach() in upload.php on line 25,

So, I re-read the article and checked the code and I’m not sure why it’s executing this section of the code as it’s supposed to be an Ajax upload.

I’ve tried it on my localhost and uploaded it to a temporary website, and I get the same result. I’m puzzled that it works for others.

@spaceshiptrooper this is what I get in the network tab in the inspector - much like Chrome.

Ah…! That suggests that $_SERVER['HTTP_X_FILENAME'] isn’t set, so the PHP script runs into the else block. This also explains why it works with the regular submit button, where $_FILES['fileselect'] is set instead. I found this thread on SO (which happens to refer to the same SP article), where the solution is to use hyphens instead of underscores for the header key.

PS: Indeed, underscores seem to be forbidden for header keys on both apache and nginx servers… I wasn’t aware of that either, so good to know. :-)

2 Likes

Thanks @m3g4p0p. Well found! It works okay now.

I’ll investigate why the status message isn’t being displayed now…

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.