Accessing JSON data in PHP from a multipartform-data request - Stack Overflow

admin2025-04-28  2

I am sending a request that contains an image file as well as meta data from a phone to a server running PHP. On the server, I would like to grab the file as well as the meta data, however, apparently if a request has Content-type multipart/form-data, you don't have access to 'php://input' as you do with 'application/json'

If at all possible, for many reasons, I would really like to do this in one request.

Here is my current non-working PHP code

$jsonString = file_get_contents('php://input');//Doesn't work
$jsonArray = json_decode($jsonString, true);
echo $jsonString; //does not throw error but echos nothing
print_r($jsonArray);//does not throw error but prints nothing

$file = $_FILES['image'];//Works
echo "File Name: " . $file['name'];//Correctly ouputs file name.

Since the code on the phone is Swift, I am not including it but basically it sends the file and also metadata in the body of the request as form data encoded as a string and delimited with boundary strings. After the initial boundary string, each item has "Content-Disposition: form-data". In the case of the file, it is also marked with "Content-Type: image/png:

What can I use in place of file_get_contents('php://input') to get access to the other form data in a multipart/form-data request?

转载请注明原文地址:http://www.anycun.com/QandA/1745780036a91189.html