Per the comments, this will be a 2 part answer.
1) When the file is uploaded, it should be stored to a specific path. The path and file name should then be stored in the database. The following example is not tested, and should be adjusted to fit 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 retrieve the file, and return it via JSON, it would look like this (again, untested):
<?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);
}
?>