Skip to content
Advertisement

Upload an image from input to server using JQuery/Ajax

I have an Input like this:
<input type="file" accept="image/*">

Now I want to send the image to the server (I guess ajax is the way to go?)
From the Server I want to save the image to an aws-s3 storage (not actually my problem)
The question is how do I send the image to php in a way that I can later store it in an object storage?

Advertisement

Answer

This code was copied from the following web page: https://www.w3schools.com/PHP/php_file_upload.asp

Note that it’s much more harder to use AJAX/jQuery, so you can use this code.

First check your php.ini file (it’s in C:/php-install-path/php.ini) and search for the following line:

JavaScript

It may appear as

JavaScript

so you need to turn in to On. Then restart your web server if it was off.

Next, create the form.

JavaScript

It will need to redirect to a PHP file as PHP can receive elements.

For the PHP file, put code like this:

JavaScript

Bonus: If you want to create a function for this, you can.

JavaScript

Then include or require the file in the PHP file that’s receiving the file upload.

JavaScript

There you go. That’s how you use PHP to upload an image.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement