How to upload file using jQuery AJAX and PHP?

Posted by Ramesh Singh | Updated on

If you want to upload a file using jQuery AJAX and PHP, then use the following code:

You need a script that runs on the server to move the file to the uploads directory. The jQuery ajax method (running in the browser) sends the form data to the server, then a script on the server handles the upload. Here’s an example using PHP.

Your HTML is fine, but update your JS jQuery script to look like this:

And now for the server-side script, using PHP in this case. upload.php: a PHP script that runs on the server and directs the file to the uploads directory:

Reference: https://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers