Page 1 of 1

Replace Stored File

Posted: Tue Sep 06, 2022 3:48 pm
by vario
How can I update the sfi_json column in zzzzsys_file? Really, I'm looking for the method used to encode the object into JSON.

Re: Replace Stored File

Posted: Tue Sep 06, 2022 4:27 pm
by kev1n
Depending on where the object or file is stored, this might help: viewtopic.php?p=24423

Re: Replace Stored File

Posted: Tue Sep 06, 2022 7:36 pm
by vario
Using this code:

Code: Select all

$image = file_get_contents($target_file);
$filesize = filesize($target_file);
$arr = array("file"=>base64_encode($image), "name"=>basename($target_file), "size"=>$filesize, "type"=>"image/png");
$json = json_encode($arr);
I get a value that will go into the database but doesn't work (i.e no image is shown). The JSON encoded string is nothing like the one that gets stored when I use the standard "Stored Files " form. For a start it has alot of "/" (all escaped) and "+" characters which are not in the "correct" encoding - I will have to do some searching to discover what's going on (I guess some modified encoding) , unless you have a quick answer?

Re: Replace Stored File

Posted: Tue Sep 06, 2022 9:04 pm
by vario
OK, cracked it. I use nuBuilder "Stored Files" to do the upload, then copy the encoded "file" string from sfi_json, and see that it is encoded using the data URI scheme https://en.wikipedia.org/wiki/Data_URI_scheme

Code: Select all

$image = file_get_contents($target_file);
$filesize = filesize($target_file);
$image64=base64_encode($image);
$arr = array("file"=>base64_encode('data:image/png;base64,' . $image64), "name"=>basename($target_file), "size"=>$filesize, "type"=>"image/png");
$json = json_encode($arr);
I can now update sfi_json with my JSON encoded variable.