JSON array containing image stored as blob

I created a json array from mySQL table using this code:

$sql = "SELECT * FROM new";

if ($result = mysqli_query($con, $sql))
{
$resultArray = array();
$tempArray = array();

while($row = $result->fetch_object())
{
$tempArray = $row;
array_push($resultArray, $tempArray);
}

echo json_encode($resultArray);
}
function_exists('json_encode');

      

This only works with text inputs and when I try to add an image (stored as a BLOB) the JSON output disappears completely. I am well aware that storing images in a database is not a great practice, but I need to have it that way. Can anyone help me with this? How can I get images as part of a JSON array?

+3


source to share


1 answer


In the comments, this will be the answer to 2 parts.

1) When the file is downloaded, it must be saved in a specific path. Then the path and file name must be stored in the database. The following example is untested and should be adjusted to suit your environment:

<?php
// PHP File Upload Handler
// Store File to specific path and Insert Path and details to DB

$uploaddir = '/var/www/uploads/';
$uploadfilepath = $uploaddir . basename($_FILES['userfile']['name']);
$uploadfilename = basename($_FILES['userfile']['name'])
$uploadfiletype = $_FILES['userfile']['type'];
$uploadfilesize = $_FILES['userfile']['size'];

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfilepath)) {
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    $stmt = $mysqli->prepare(""INSERT INTO mediaTable (name, path, type, size) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("sssi", $uploadfilename, $uploadfilepath, $uploadfiletype, $uploadfilesize);
    $stmt->execute();
    $stmt->close();
    $mysqli->close();
    $results = array(
        "status" => "success",
        "name" => $uploadfilename,
        "type" => $uploadfiletype,
        "size" => $uploadfilesize,
        "path" => $uploadfilepath
    );
} else {
    $results = array(
        "status" => "error",
        "Could not save uploaded file."
    );
}
?>

      



2) When we want to get a file and return it via JSON it will look like this (again, not checked):

<?php
// Get Media file (POST input) from database and return data via JSON

if(isset($_POST['fn'])){
    $filename = $_POST['fn'];
    $results = array();
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    $stmt = $mysqli->prepare("SELECT name, path, type, size FROM mediaTable WHERE name=?");
    $stmt->bind_param("s", $filename);
    $stmt->execute();
    $stmt->bind_result($fname, $fpath, $ftype, fsize);
    while ($stmt->fetch()) {
         $results[] = array("name" => $fname, "path" => $fpath, "type" => $ftype, "size" => $fsize, "base64" => '');
    }
    $stmt->close();
    $mysqli->close();

    foreach($results as $file){
        $file['base64'] = file_get_contents($file['path']);
    }
} else {
    $results["error"] = "No file submitted";
}

if(isset($results)){
    echo json_encode($results);
}
?>

      

0


source







All Articles