INDEX

PHP - Image file insertion into XML

Sample code to insert the image file to XML.

< ?
$strFileInfo = "input_img.jpg";
$imgtxt_base64_encode = base64_encode(file_get_contents($strFileInfo));

$dom = new DomDocument('1.0', 'utf-8');
$imgs = $dom->appendChild($dom->createElement('imgs'));
$img  = $imgs->appendChild($dom->createElement('img'));

$title = $img->appendChild($dom->createElement('title'));
$title->appendChild($dom->createTextNode('This is sample image.'));

$fname = $img->appendChild($dom->createElement('fname'));
$fname->appendChild($dom->createTextNode('input_img.jpg'));

$imgtxt = $img->appendChild($dom->createElement('imgtxt'));
$imgtxt->appendChild($dom->createTextNode($imgtxt_base64_encode));
$dom->formatOutput = true;
echo $dom->save('test01.xml');
?>
----------------------------------------------
(result : test01.xml)
< ?xml version="1.0" encoding="utf-8"?>
< imgs>
  < img>
    < title>This is sample image.< /title>
    < fname>input_img.jpg< /fname>
    < imgtxt>/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAs  ...
.....
............... dMclY//2Q==< /imgtxt>
  < /img>
< /imgs>


Copyright(c) 2007-2023 coding.dojeun.com All Rights Reserved.