POST image from device to PHP

Ok… Who wants to work together to figure this issue out?

While Corona 'n staff can’t seem to provide any help or scripting on this (judging from the variety of posts I was able to find here), we should be able to come up with a solution here. I will be happy to share any my success and setup a code exchange page for it. :wink:

So, from those various posts I did find, plus a couple lua reference sites, this seems to be the way to get a file loaded and sent to a web page…

 -- get file, find length.  
 local path = system.pathForFile("test\_img.png" )  
 local fh = io.open(path)  
 local len = fh:seek( "end", 0 )  
 io.close(fh)  
  
 local url = "http://www.somesite.com/post\_img.php";  
  
 http = require("socket.http")  
 ltn12 = require("ltn12")  
  
 local response = {}  
 http.request{  
 url = url,  
 method = "POST",  
 headers = {  
 ["Content-Type"] = "application/x-www-form-urlencoded",  
 ["Content-Length"] = len -- this len was found above.  
 },  
  
 source = ltn12.source.file(io.open(path)),  
 sink = ltn12.sink.table(response)  
 }  
  
 -- show the response from page.  
 print (response[1]);  
  

And then on the server side, I’ve simply got …

<?php <br>  
 foreach ($\_POST as $k =\> $v) {  
 $filename = "testImg" . time() . ".png";  
 $img\_file = fopen ($filename, "w");  
 fwrite ($img\_file, $v);  
 fclose ($img\_file);  
  
 echo "Received $filename";  
 }  
  

So, it does send a file from the device to the php script… but the size of what I get is not the same as what I’m sending.

I’m *pretty sure* this is a simple issues of the encoding as whatever it’s sending is definitely the image file… but I’m seriously pounding my head against the wall trying to sort out how to correctly encode on the device and decode on the php side.

Any one with any thoughts? :slight_smile:
[import]uid: 13859 topic_id: 11644 reply_id: 311644[/import]

I had been thinking about doing this for a while, thanks for the push :slight_smile:

local mime = require "mime"  
local path = system.pathForFile( "tojam6-AngeloYazar.png" )  
local fileHandle = io.open( path, "rb" )   
if fileHandle then   
 local params = {  
 headers = {["Content-Type"] = "multipart/text"},  
 body = mime.b64( fileHandle:read( "\*a" ) ),  
 }  
 io.close( fileHandle )   
  
 local function networkListener ( event )  
 print( event.isError or event.response )  
 end  
 network.request( "http://www.zeppelinnations.com/apps/pngup.php", "POST", networkListener, params)  
end  
<?php <br>$pngdata = $HTTP\_RAW\_POST\_DATA;  
if( isset( $pngdata ) ) {  
 $filename = "testImg.png";  
 $img\_file = fopen ($filename, 'wb');  
 fwrite ($img\_file, base64\_decode( $pngdata ));  
 fclose ($img\_file);  
 echo "success";  
}  
else echo "failed";  
?\>  

This works on my server, hopefully it works out for you.

-Angelo
[import]uid: 12822 topic_id: 11644 reply_id: 42381[/import]

Angelo - this is awesome and works perfectly.

As you got much further than I did – you should really be the one to post it in the code exchange… If you don’t want to and don’t mind if I do then I’ll take care of it … But one way or another, this needs to be someplace where people can find it easily.

Thank you again – this was a huge stumbling block now out of the way! :slight_smile:

~~Kenn [import]uid: 13859 topic_id: 11644 reply_id: 42406[/import]

Hey Kenn, I’m glad it helps! - please, go ahead and post it. You could write a nice little wrapper for it too if you want. Something like uploadImage( filename, url, onComplete )

-Angelo [import]uid: 12822 topic_id: 11644 reply_id: 42422[/import]

Great - all done!

http://developer.anscamobile.com/code/upload-binary-corona-php-script

Thanks! :slight_smile:

[import]uid: 13859 topic_id: 11644 reply_id: 42432[/import]

Nice! Looks good.

-Angelo [import]uid: 12822 topic_id: 11644 reply_id: 42464[/import]

Thats going to get a bit heavy isn’t it?

What we really need is a true multipart post, which is what most servers would be accepting.

[import]uid: 22381 topic_id: 11644 reply_id: 62809[/import]