William Wino
William Wino

Reputation: 3819

How do I save an image file from json string which contains the image file?

This is a bit confusing. I use w2ui framework as my user interface and it sends JSON string when it posts a form that contains a file input. Now the JSON string looks like this:

{
    "cmd":"save",
    "recid":0,
    "record":
    {
        "id":"2",
        "image":
        [
            {
                "name":"test.jpg",
                "type":"image/jpeg",
                "modified":"2016-04-03T15:54:12.638Z",
                "size":31216,
                "content":"very_long_content_string"
            }
        ]
    },
    "id":"2",
    "image":
    [
        {
            "name":"test.jpg",
            "type":"image/jpeg",
            "modified":"2016-04-03T15:54:12.638Z",
            "size":31216,
            "content":"very_long_content_string"
        }
    ]
}

I don't know why the information is redundant. The question is how do I somehow save the file inside the image array into my local disk?

Upvotes: 2

Views: 2927

Answers (1)

Faisal Ashfaq
Faisal Ashfaq

Reputation: 2678

Parse the JSON and use:

<?php
$base64string = very_long_content_string;
file_put_contents('img.png', base64_decode($base64string));

?>

Upvotes: 2

Related Questions