Trying to upload an image and getting an error

Will this get any image to the uploads folder ?

 -- Selection completion listener local function onComplete( event ) local photo = event.target if photo then print( "photo w,h = " .. photo.width .. "," .. photo.height ) local multipart = MultipartFormData.new() local path = system.pathForFile( userName .. ".jpg", system.TemporaryDirectory ) multipart:addFile("Image", path, "uploads/", userName .. ".jpg") local params = {} params.body = multipart:getBody() params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called. end end local function pickPhoto( event ) media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = onComplete, origin = button.contentBounds, permittedArrowDirections = { "right" }, destination = { baseDir=system.TemporaryDirectory, filename= userName .. ".jpg" } }) end

I don’t see where you’re actually telling Corona to upload the file.  It looks like you copied code from:

https://stackoverflow.com/questions/45580741/how-to-upload-an-image-using-corona-sdk-lua

There is a network.request() line in that post that’s missing from your code. At some point you have to call your server script to handle the upload.

Also, you’re only showing us the Corona side, not the PHP side. We have no idea of knowing if you even have an “uploads” folder in the same folder where the PHP script runs from and if you have permission to write files to that folder.

Rob

This thread is like me trying to build a house when the closest I’ve got to DIY was putting up a corner shelf badly and owning a box of nails.

It would just end with me limbless in a cement-laden swamp, next to a pile of bricks, wood and glass.

That’s why I don’t build houses, and if I was going to learn I’d probably start with a table rather than an 112-room mansion.

Brilliant :slight_smile:

Something I’ve just noticed…  The OP has made over 900 posts, and yet this is in the ‘Newbie Questions’ forum.

Everyone learns in different ways and at different rates. Some people ask a lot of questions others need less help. Lets keep in mind that we are all different but share a common goal of wanting to develop with Corona.  

As always, you are at your own discretion when you choose to help answer questions and we are very grateful when you do, but calling developers out isn’t productive or helpful for the health of the community.

Thank you for understanding

Rob

Ordinarily I’d agree Rob and everyone needs a helping hand sometimes, but this particular poster doesn’t seem to be learning anything, or indeed interested in learning anything. He wants ready-made code and us (you in this case, since September) to do all the work. At least you’re being paid for it! :slight_smile:

None of the nuggets of wisdom dished out on other threads on various other projects is ever applied to the next problem, because it is ignored. The resulting solution is plugged in and if it works great, if it doesn’t, it’s back to the thread (or stackoverflow).

I will say no more on the subject as the message doesn’t seem to get across anyway, I’m off to housebuilder.com to ask how to do a loft conversion. Can’t be that hard, just chuck some wood and a hammer at it?

Is there anyway to keep people from posting on my posts or block them ?

yeah I didn’t post my php code because you said we should take care of this side first . But here it is ?

ini\_set('display\_errors', 1); ini\_set('display\_startup\_errors', 1); error\_reporting(E\_ALL); session\_start(); $con = new mysqli("localhost", "hashx10h\_brandon", "bigman23", "hashx10h\_hash"); if ($con-\>connect\_error) { die("Connection failed: " . $con-\>connect\_error); } \<?php $username = isset($\_SESSION['username']) ? $\_SESSION['username'] : ""; $info = date('Y-m-d\_H-i-s'); if(!empty($username)) { if (isset($\_FILES['fileToUpload'])) { $errors= array(); $file\_name = $\_FILES['fileToUpload']['name']; $file\_size = $\_FILES['fileToUpload']['size']; $width = 1500; $height = 1500; $file\_tmp = $\_FILES['fileToUpload']['tmp\_name']; $file\_type = $\_FILES['fileToUpload']['type']; $tmp = explode('.',$\_FILES['fileToUpload']['name']); $file\_ext=strtolower (end ($tmp)); $extensions= array("jpeg","jpg","png"); if(in\_array($file\_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a JPEG or PNG file."; } if ($file\_size \> 8097152) { $errors[] = 'File size must be 2 MB'; } if ($width \> 1500 || $height \> 1500) { echo"File is to large"; } if(empty($errors)==true) { move\_uploaded\_file($file\_tmp,"uploads/".date('Y-m-d\_H-i-s').$file\_name); $store = "UPDATE users SET userPic='".mysqli\_real\_escape\_string( $conn, date('Y-m-d\_H-i-s').$file\_name )."', date\_time='" . $info . "' WHERE username='".mysqli\_real\_escape\_string( $conn, $username )."'"; header('Location: http://localhost/interpage/profile.php'); if(mysqli\_query($conn, $store)) { } else { echo "Upload failed!"; } }else{ print\_r($errors); echo"Couldn't upload picture"; } }} else { echo "Invalid Username"; } ?\> \<?php $getimg = mysqli\_query($conn,"SELECT userPic, date\_time FROM users WHERE username='".mysqli\_real\_escape\_string( $conn, $username )."'"); $rows=mysqli\_fetch\_array($getimg); $img = $rows['userPic']; ?\>

This is code I just entered . I’ll try it now . (I posted because maybe you’ll catch something I won’t).

No, we can’t block people from posting on your posts. You don’t want that.

On your Corona side. This line appears to be incorrect too:

multipart:addFile("Image", path, "uploads/", userName .. ".jpg")

The third parameter isn’t a path, it appears to be a “MIME Type”. It should be “image/jpg” like in the example

multipart:addFile("Image", path, "image/jpg",&nbsp;userName ..&nbsp;".jpg")

I’ll have to look at the PHP later, but it looks like it has problems too.

Rob

UPDATED PHP CODE :

ini\_set('display\_errors', 1); ini\_set('display\_startup\_errors', 1); error\_reporting(E\_ALL); session\_start(); $con = new mysqli("localhost", "hashx10h\_brandon", "bigman23", "hashx10h\_hash"); if ($con-\>connect\_error) { die("Connection failed: " . $con-\>connect\_error); } $username = isset($\_SESSION['username']) ? $\_SESSION['username'] : ""; $info = date('Y-m-d\_H-i-s'); if(!empty($username)) { if (isset($\_FILES['fileToUpload'])) { $errors= array(); $file\_name = $\_FILES['fileToUpload']['name']; $file\_size = $\_FILES['fileToUpload']['size']; $width = 1500; $height = 1500; $file\_tmp = $\_FILES['fileToUpload']['tmp\_name']; $file\_type = $\_FILES['fileToUpload']['type']; $tmp = explode('.',$\_FILES['fileToUpload']['name']); $file\_ext=strtolower (end ($tmp)); $extensions= array("jpeg","jpg","png"); if(in\_array($file\_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a JPEG or PNG file."; } if ($file\_size \> 8097152) { $errors[] = 'File size must be 2 MB'; } if ($width \> 1500 || $height \> 1500) { echo"File is to large"; } if(empty($errors)==true) { move\_uploaded\_file($file\_tmp,"uploads/".date('Y-m-d\_H-i-s').$file\_name); $store = "UPDATE users SET userPic='".mysqli\_real\_escape\_string( $con, date('Y-m-d\_H-i-s').$file\_name )."', date\_time='" . $info . "' WHERE username='".mysqli\_real\_escape\_string( $con, $username )."'"; echo"uploaded"; if(mysqli\_query($con, $store)) { } else { echo "Upload failed!"; } }else{ print\_r($errors); echo"Couldn't upload picture"; } }} else { echo "Invalid Username"; } $getimg = mysqli\_query($con,"SELECT userPic, date\_time FROM users WHERE username='".mysqli\_real\_escape\_string( $con, $username )."'"); $rows = mysqli\_fetch\_array($getimg); $img = $rows['userPic'];

Also no errors are being printed in my php code 

Is this code any better ? 

 -- Selection completion listener local function onComplete( event ) local photo = event.target if photo then print( "photo w,h = " .. photo.width .. "," .. photo.height ) local multipart = MultipartFormData.new() local path = system.pathForFile( userName .. ".jpg", system.TemporaryDirectory ) multipart:addFile("Image", path, "image/jpg", userName .. ".jpg") local params = {} params.body = multipart:getBody() params.headers = multipart:getHeaders() -- Headers not valid until getBody() is called. network.request("https://http://hash.x10host.com/cgi-bin/hash/upload.php/services/imageupload?parameter=1", "POST", listener, params) end end local function pickPhoto( event ) media.selectPhoto( { mediaSource = media.SavedPhotosAlbum, listener = onComplete, origin = button.contentBounds, permittedArrowDirections = { "right" }, destination = { baseDir=system.TemporaryDirectory, filename= userName .. ".jpg" } }) end

Also the way the code it setup now, will it go into the uploads folder or sandbox ?

Passes nick some nails and a hammer… now if you could reply with the full code to build Canary Wharf that would be great!

I was just reading the docs and it says  Most existing scripts that accept file upload probably won’t work with network.upload() because they’re looking for an HTTP POSTform-based MIME multi-part upload format. 

Does that mean I have to change all of my POST’S to PUT ?

If you’re using the multipart mime library, you want to use POST. You’re not using network.upload().

The Corona side is almost there.  This line has an error in it:

network.request("https://http://hash.x10host.com/cgi-bin/hash/upload.php/services/imageupload?parameter=1", "POST", listener, params)

You can’t have https:// and http:// in the same URL. I don’t know if your server is an http: or https: server. You need to figure that out on your own.

Now looking at your PHP script, I don’t know what fields will actually be passed to it. However, you are running a script at the path:

/cgi-bin/hash/upload.php.

Your script is trying to write to a folder named:

/cgi-bin/hash/upload/

which I do not know if it exists but I can’t see any web host allowing you to write arbitrary files into the /cgi-bin path. No one here can help you with the server side unless you are willing to give them access to your server and probably pay them to help you.

Rob

Oh wait . The cgi-bin path is where the upload.php script is . I have a separate folder called uploads for the images uploaded in my corona project folder .

Is this better ? 

network.request("http://hash.x10host.com/cgi-bin/hash/upload.php?parameter=1", "POST", listener, params)

Does this 

        local path = system.pathForFile( userName … “.jpg”, system.TemporaryDirectory )

        multipart:addFile(“Image”, path, “image/jpg”, userName … “.jpg”)

send any jpg image the user tries to upload in the sandbox ?