Question

I have problem with upload file to a server with php.

This is my code testForm.php

    $uploaddir = 'uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        $sta["status"] = "Success";
        echo stripslashes(json_encode($sta)); 
    }

And in livecode

 put the text of field "pF" into pathFile
 put urlEncode("file:"& pathFile) into tFile
 put libUrlFormData("userfile",tFile) into tArgList
 post tArgList to URL "http://localhost:9999/livecodeTestJson/testForm.php"

It's not working.

Was it helpful?

Solution

I do not know, if your php code is correct, but your livecode part isn´t.

To post files to a webserver you need the libUrlMultipartFormData function.

Below you find a sample from the libURL documentatin. I adjusted it a little bit using your values

 put empty into tFormData
    put "http://www.someserver.com/cgi-bin/form.cgi" into tUrl
    put the text of field "pF" into pathFile
    put "<file>" & pathfile into tFile
    if libUrlMultipartFormData(tFormData, "userfile", tFile) is not empty then
      answer it ##error
    else
      set the httpHeaders to line 1 of tFormData
      post line 2 to -1 of tFormData to url tUrl 
      ## check the result, etc., here
      set the httpHeaders to empty
    end if

If you also want post form data then you have to include that data in the call of the libURLMultiPartFormData.

Let´s say you have the form fields "name", "email" and "message" and the variables tName, tEmail and tMessage contain the data then you would replace line 5 of the sample script with the following line

 if libUrlMultipartFormData(tFormData, "name", tName, "email", tEmail, "message", tMessage, "userfile", tFile) is not empty
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top