How to upload image to server

When the user clicks the upload picture button , I want them to select a picture which will then be saved to the images folder in my corona app project and then be saved to the database so it can be shown on the users profile page on my corona app . I have only done a small part to this which is giving the user the option to view their photos . Can someone help me with the rest or if you have an easier way I would follow . Here is what I have so far :

profile.lua :

 -- Selection completion listener local function onComplete( event ) local photo = event.target if photo then print( "photo w,h = " .. photo.width .. "," .. photo.height ) end end local button = widget.newButton( { shape = "roundedRect", left = 70, top = 350, id = "pfp", label = "Upload picture", onEvent = pickPhoto, fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } }, labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } } } ) local function pickPhoto( event ) media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = onComplete, origin = button.contentBounds, permittedArrowDirections = { "right" }, destination = { baseDir=system.TemporaryDirectory, filename="image.jpg" } }) end local MultipartFormData = require("class\_MultipartFormData") local multipart = MultipartFormData.new() local path=system.pathForFile( "image.jpg", system.TemporaryDirectory ) multipart:addFile("Image", path, "image/jpg", "image.jpg") local params = {} params.body = multipart:getBody() params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called. network.request("https://your.server.url/services/imageupload?parameter=1", "POST", listener, params)

class_MultipartFormData.lua :

https://github.com/benglard/Augment/blob/master/App/class_MultipartFormData.lua

I have all the code there in mine .

upload.php :

\<?php ini\_set('display\_errors', 1); ini\_set('display\_startup\_errors', 1); error\_reporting(E\_ALL); $servername = "localhost"; $username = "username"; $password = "password"; $database = "db"; $con = new mysqli($servername, $username, $password, $database); if($con == true) { } // Check connection if ($con-\>connect\_error) { die("Connection failed: " . $con-\>connect\_error); } include("auth\_login.php"); $target\_dir = "images/uploads/"; $target\_file = $target\_dir . basename($\_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target\_file,PATHINFO\_EXTENSION); // Check if image file is a actual image or fake image if(isset($\_POST["change"])) { move\_uploaded\_file($\_FILES["fileToUpload"]["tmp\_name"], $target\_file); if($check !== false) { echo "picture uploaded"; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } ?\>

please help me thanks

I have this code in my profile.lua :

local MultipartFormData = require("class\_MultipartFormData") local multipart = MultipartFormData.new() local widget = require("widget") -- forward declare the text fields local json = require("json") local path=system.pathForFile( "image.jpg", system.TemporaryDirectory ) multipart:addFile("Image", path, "image/jpg", "image.jpg") local params = {} params.body = multipart:getBody() params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called. network.request("https://your.server.url/services/imageupload?parameter=1", "POST", listener, params) local userName = composer.getVariable( "username" ) local function networkListener( event ) if ( event.isError ) then local alert = native.showAlert( "Error Loading .", "Check your internet connection .", { "Try again" } ) end end -- Selection completion listener local function onComplete( event ) local photo = event.target if photo then print( "photo w,h = " .. photo.width .. "," .. photo.height ) end end local button = widget.newButton( { shape = "roundedRect", left = 70, top = 350, id = "pfp", label = "Upload picture", onEvent = pickPhoto, fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } }, labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } } } ) local function pickPhoto( event ) media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = onComplete, origin = button.contentBounds, permittedArrowDirections = { "right" }, destination = { baseDir=system.TemporaryDirectory, filename="image.jpg" } }) end

I have the code that’s here https://github.com/benglard/Augment/blob/master/App/class_MultipartFormData.lua in a separate file . 

When I run the code , I get this error :

ERROR: Runtime error 09:48:35.955 C:\Users\user\Documents\Corona Projects\app\class\_MultipartFormData.lua:102: assertion failed! 09:48:35.955 stack traceback: 09:48:35.955 [C]: in function 'error' 09:48:35.955 ?: in function 'gotoScene' 09:48:35.955 C:\Users\user\Documents\Corona Projects\app\newsfeed.lua:43: in function '\_onPress' 09:48:35.955 ?: in function '\_setSelected' 09:48:35.955 ?: in function '?' 09:48:35.955 ?: in function \<?:182\>

Hello any help ?

I’ve been there before. Just wait a little longer and surely someone will help you. I don’t know anything about this myself.

I found a sample within Corona’s sample folder, it’s pretty much everything you need to know about Instagram’s camera. :slight_smile:

When you are on the Corona Simulator, click Samples, then click the Media Folder, then click the Camera folder, and launch the main.lua file.

Did you find any assistance?

No not yet.

I have this code in my profile.lua :

local MultipartFormData = require("class\_MultipartFormData") local multipart = MultipartFormData.new() local widget = require("widget") -- forward declare the text fields local json = require("json") local path=system.pathForFile( "image.jpg", system.TemporaryDirectory ) multipart:addFile("Image", path, "image/jpg", "image.jpg") local params = {} params.body = multipart:getBody() params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called. network.request("https://your.server.url/services/imageupload?parameter=1", "POST", listener, params) local userName = composer.getVariable( "username" ) local function networkListener( event ) if ( event.isError ) then local alert = native.showAlert( "Error Loading .", "Check your internet connection .", { "Try again" } ) end end -- Selection completion listener local function onComplete( event ) local photo = event.target if photo then print( "photo w,h = " .. photo.width .. "," .. photo.height ) end end local button = widget.newButton( { shape = "roundedRect", left = 70, top = 350, id = "pfp", label = "Upload picture", onEvent = pickPhoto, fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } }, labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } } } ) local function pickPhoto( event ) media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = onComplete, origin = button.contentBounds, permittedArrowDirections = { "right" }, destination = { baseDir=system.TemporaryDirectory, filename="image.jpg" } }) end

I have the code that’s here https://github.com/benglard/Augment/blob/master/App/class_MultipartFormData.lua in a separate file . 

When I run the code , I get this error :

ERROR: Runtime error 09:48:35.955 C:\Users\user\Documents\Corona Projects\app\class\_MultipartFormData.lua:102: assertion failed! 09:48:35.955 stack traceback: 09:48:35.955 [C]: in function 'error' 09:48:35.955 ?: in function 'gotoScene' 09:48:35.955 C:\Users\user\Documents\Corona Projects\app\newsfeed.lua:43: in function '\_onPress' 09:48:35.955 ?: in function '\_setSelected' 09:48:35.955 ?: in function '?' 09:48:35.955 ?: in function \<?:182\>

Hello any help ?

I’ve been there before. Just wait a little longer and surely someone will help you. I don’t know anything about this myself.

I found a sample within Corona’s sample folder, it’s pretty much everything you need to know about Instagram’s camera. :slight_smile:

When you are on the Corona Simulator, click Samples, then click the Media Folder, then click the Camera folder, and launch the main.lua file.

Hi, Perhaps consider Develephant’s “Amazon S3 Lite” plugin in the Corona Marketplace. https://marketplace.coronalabs.com/plugin/s3-lite

I ran into complications with class_MultipartFormData.lua too for image uploads, and this plugin solved my problem.  You’d have to learn a little about Amazon S3 and its permissions (they have good documentation pdfs), but it’s worth it

Did you find any assistance?

No not yet.

Hi, Perhaps consider Develephant’s “Amazon S3 Lite” plugin in the Corona Marketplace. https://marketplace.coronalabs.com/plugin/s3-lite

I ran into complications with class_MultipartFormData.lua too for image uploads, and this plugin solved my problem.  You’d have to learn a little about Amazon S3 and its permissions (they have good documentation pdfs), but it’s worth it