Using HttpUtils or HttpClient and the AdvancedCamera library I can POST images to a server as a byte array (using HttpClient.InitializePost2) or as a file (HttpUtils.PostFile)
But in PHP I have absolutely no idea how I'd create a "listener" to read in the byte array or file until every byte has been read in.
Thanks guys. This works but the image file taken from the camera ends up being 0 bytes on my server.
See B4A code I am using to generate the request.
B4X:
Sub Camera1_PictureTaken (Data() As Byte)
camera1.StartPreview
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "abc123.jpg", False)
out.WriteBytes(data, 0, data.Length)
out.Close
'ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "1.jpg"), True)
takepic_button.Enabled = True
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Add files
Dim files As List
files.Initialize
Dim FD As FileData
fd.Initialize
fd.Dir = File.DirRootExternal
fd.FileName = "abc123.jpg"
fd.KeyName = "upfile"
fd.ContentType = "image/jpeg"
files.Add(fd)
'Add name / values pairs (parameters)
Dim NV As Map
NV.Initialize
NV.Put("note1", "abc")
NV.Put("note2", "def")
Dim req As HttpRequest
req = MultipartPost.CreatePostRequest(".............postimage.php", NV, files)
hc.Execute(req, 10)
''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
Edit: Realized that I am sending more than just the file in this request which may confuse php://input . I am going to see if PostFile works better.
Edit2: PostFile works great. But I still need to be able to send other information such as name/value pairs and extract each of these (NV pairs, files) from the php://input stream.
I am using PostFile in HttpUtils and timwil's PHP code (attached in post 3 of this thread) to send a file to my server via https.
The phone seems to transmit, and the server notes the correct filename, but the file size is 0 bytes. The only references to this "0 byte" problem I've found on the forum relate to another method of sending files, but not for PostFile.
I've tried a text file and a SQLite file and get the same result (I'm not sending an image from the camera, as timwil does, but the files I'm sending do exist and have more than 0 bytes).
I checked the error log on my server (which I should have done before posting -- I apologize). It turns out that my web host had disabled fwrite. Once enabled, everything works!
I have a problem, i need to upload, a file/data from B4A , such as date&time, temperature,rh,lattitude,longitude, ..and want to upload all of these , to my website, but don't know, what should i do ? i mean, in B4A and in Php , please help me, i'm a newbie...
thank you so much...
Are you uploading a file, or are you uploading data values, or both?
If it's just the values, then you can do that using a simple request with http2utils; for example, use PostString. If you want to include an actual file, like a picture, as well, then you need to do a multi-part request, and there are lots of examples of that which can be found by searching the forums.
On the PHP side, if you POST your data, then it will all be available to your script in the $_POST array, for example if you post a value with
B4X:
Dim poster As HttpJob
poster.Initialize("jobName",Me"
poster.PostString(someURL,"TEMP=" & temperature & "&HUMIDITY=" & humidity)
Then in the PHP script, the value $_POST['HUMIDITY'] would have the humidity, and $_POST['TEMP'] the temperature.
If you're uploading files, then those appear in the $_FILES array for your PHP code; suppose the name you gave one in your form data as 'newpicture' then you could see if there was a file uploaded in the script, and check it's type, with something like this
PHP:
if ( is_uploaded($_FILES['newpicture']['tmp_name'])) {
if ( $_FILES['newpicture']['type'] != 'image/jpeg' ) {
// not a jpeg, perhaps create an error message
} else {
// move it somewhere
move_uploaded_file($_FILES['newpicture']['tmp_name'],'/some/path/to/newfilename.jpg') ;
}
}