Monday, May 20, 2024

How to upload image with react js and php?

Upload image with react js and php

Hello, welcome to In this post, I will tell you, How to upload image with react js and php?  Reactjs is a Javascript Library to build user interface.

We will upload image through reactjs in php with move_uploaded_file function in php and this is very interesting. 

I did react js and php collaboration in my post and this is very good. 

Here is the working and tested code in reactjs and you can add this into your index.js file:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class Hello extends React.Component{
  state = {
    selectedFile : null
  fileSelect = event => {
  fileUpload = () => {
    const fd = new FormData();
    fd.append('image', this.state.selectedFile,;'http://localhost/core_php.php', fd

  render() {
    return (
    <input type="file" onChange = {this.fileSelect} />
  <button onClick = {this.fileUpload}>Upload</button>

  ReactDOM.render(<Hello />, document.getElementById('root'));
 Here is the code for php and you can add this into your code_php.file and you also need to make img folder also:
move_uploaded_file($_FILES["image"]["tmp_name"], "img/" . $_FILES["image"]["name"]);

 If you have any query related to this code and you can comment below. I will come with more reactjs posts.

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.