React Native Upload Image to Server using PHP MySQL iOS Android Tutorial

Image uploading is one of the most usable functionality in react native development area because in today scenario every android and iOS application is fully dynamic and comes with Login functionality where user can set his own image as profile picture or upload images from his application. So in this tutorial we would going to create a react native application to Upload Image to Server using PHP MySQL and Store Image URL in Database so it can be accessible for further use from directly server.

What we are doing in this project : We would store the image into a folder created in our hosting server root directory and after successfully done the uploading process we would store the complete image URL(Path of Server) into MySQL database using PHP scripting language after that we would show a response message to user in react native application.

List of React Native libraries used in this project :

  1. react-native-image-picker  : To pick image form gallery or camera.
  2. rn-fetch-blob : Send selected image to server.

Contents in this project React Native Upload Image to Server using PHP MySQL-Store Image URL in Database iOS Android Example Tutorial:

1. Installing the react-native-image-picker library :

1. Before getting started we need to install the react-native-image-picker library in our current project. To install the react-native-image-picker library you need to open your react native project folder in Command Prompt or Terminal and execute below command .


2. After executing the above command it will start downloading and installing the react-native-image-picker library. Below is the screenshot of CMD after successfully install the library.

3. After installing the library we need to execute the 
reactnative link command , This command will refresh and re-index the complete react native project folder and index the react native image picker library in your current project.

4. Configure Project for Android devices :

1. Open build.gradle file present in your folder located in YourReactNativeProject/android.

2. Replace classpath ‘com.android.tools.build:gradle:2.2.+’ on previous line.

3. Now open YourReactNativeProjectFolder/android/gradle/wrapper/gradle-wrapper.properties file and replace previous distributionUrl line to this distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip  .Below is the complete source code of gradle-wrapper.properties file.

4. Finally Open AndroidManifest.xml file present inside YourReactNativeProjectFolderandroidappsrcmain and Add CAMERA permission and WRITE_EXTERNAL_STORAGE permission. Below is the final code of AndroidManifest.xml file.

5. Configure Project for iOS devices :

1. Open YourReactNativeProject -> ios – > YourProjectName.xcodeproj file.

2. Now we need to open info.plist file from side panel, This file contain all the useful information about your project.

3. Click on the Small + Plus icon present on the top.

4. Select the Privacy – Camera Usages Description permission like i did in below screenshot.

5.  Select Privacy – Photo Library Usages permission .

6. Now we need to enter some info text in front of both permissions like i did in below screenshot so we remember what is the purpose of this permission.

2. Installing the rn-fetch-blob library :

1. Open your react native project folder in CMD and execute below command like i did in below screenshot.

2. After successfully install the library we need to execute 
reactnative link command in order to refresh our complete project and index the react native fetch blob library.

3.  Creating MySQL database+Table on your PhpMyAdmin Server :

1. Create a new database in your PHPMYADMIN control panel named as mydatabase .

2. Inside this database we need to make a table named as image_upload_table with 3 columns idimage_tag and image_path. See the below MySQL table screenshot to know how to set their values. This table is used to store the complete image URL with domain name and a image tag.

4.  Creating PHP Script to receive and store send image on Server and Insert image URL in MySQL database:

I have created a folder name as Project and inside the folder i have created another folder name as Uploads . So all the images will upload inside the Uploads folder.

Code for upload_image.php file:

5.  Start Coding of React Native :

1. Open your project’s App.js file.

2. Import StyleSheet, Text, View, PixelRatio, TouchableOpacity, Image, TextInput and Alert component in your project.

3. Import ImagePicker and RNFetchBlob module from react-native-image-picker and rn-fetch-blob library .

4. Create  a class named as Project in your App.js file.

5. Create constructor() in your project folder with 3 States named as ImageSource, data and Image_TAG.

ImageSource : Used to hold the image path when we select image from gallery or capture image form camera.

data : Used to hold the selected image as object to send on sever.

Image_TAG : Used to hold the image tag, some text send with image.

6. Create two functions named as selectPhotoTapped and ImagePicker.showImagePicker , these both functions is predefined inbuilt functions of react native image picker library.

7. Create another function named as uploadImageToServer().  Inside this function we would simply use the RNFetchBlob.fetch() method to upload the selected image on our server. We are testing this application on our localhost so i am putting my IP address here.

8. Create 2 Touchable opacity component and 1 Text Input component inside render’s return block .

9. Creating Style.

10. Complete source code for App.js File :

Screenshots:

Upload Image to Server using PHP MySQL iOS Android Tutorial