PHP code interaction with HttpUtils.PostFile / HttpClient.InitializePost2

andrewtheart

Member
Licensed User
Longtime User
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.
 

timwil

Active Member
Licensed User
Longtime User
Attached is a .zip file containing a short php script that receives the file on a server and writes it out.

I tried to post it as text here but I would not let me - I guess because it was php code.


This works for me - it gets the filename using

/msgmeupload.php?FileName=Test

and writes it out to

./media/FileName
 

Attachments

  • msgmeupload.zip
    314 bytes · Views: 1,110
Upvote 0

andrewtheart

Member
Licensed User
Longtime User
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.
 
Last edited:
Upvote 0

andrewtheart

Member
Licensed User
Longtime User
I don't think that it is broken. However it is simpler to send the file as the POST data and send the parameters as GET parameters.

OK, thanks. Still not clear on how you'd extract both NV pairs and POST data (like files) from MultipartPost.CreatePostRequest
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
OK, thanks. Still not clear on how you'd extract both NV pairs and POST data (like files) from MultipartPost.CreatePostRequest

Hi.

Handle your POST data as you are already doing then use the predefined PHP $_GET array to get any name/value pairs from the URL.

PHP: $_GET - Manual

I don't think there is any problem using both GET and POST methods with a single request.

Martin.
 
Upvote 0

isr

Member
Licensed User
Longtime User
using PostFile, but file size on server = 0 bytes

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).

Here is the code I'm using in B4A for PostFile:


HttpUtils.PostFile("test", "https://www.myserver.com/filereceive.php?FileName=testfile.txt", File.DirDefaultExternal, "testfile.txt")

I can access the file "testfile.txt" in the directory listed, and it's not empty.

What should I be trying to do to troubleshoot this problem?
 
Last edited:
Upvote 0

isr

Member
Licensed User
Longtime User
Thank you, Erel!

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!
 
Upvote 0

agus mulyana

Member
Licensed User
Longtime User
Hi Erel,

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...
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
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') ;
  }
}
 
Upvote 0
Top