Jump to content
Larry Ullman's Book Forums

The Visibility Of The $_Files Super Global & Ajax


Recommended Posts

Hi Larry,

 

I regularly use the move_uploaded_file function as described in chapter 11 of the book.  However the other day I wanted to use it in a form the data of which gets processed via a JQuery AJAX POST call.

 

The form enables the user to select a file for uploading and I was wanting the AJAX-called PHP script to then process it including the move-uploaded_file logic.  But it seems that the $_FILES super global values are not visible to the AJAX-called PHP script.  Does this sound right?

 

FYI the $_FILES data are visible if I use your technique where the form script calls itself with an 'is submitted' check. (Page 91....).

 

Your insights will be appreciated.

 

Thanks, and Cheers from Oz.

Link to comment
Share on other sites

Necuima, your observations are right on the money. You cannot use Ajax to upload files (well, you can, but not like you think).

The following Stack Overflow question/answers should be exactly what you're looking for:

http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload

 

Basically, you either need to use an iframe hack, which is supported in all browsers, or you need to use the new HTML5 XHR2 FormData object, which is unfortunately not supported in IE9 and below (as can be readily seen from the following link):

http://caniuse.com/xhr2

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...