PHP 中 XML 嵌入 base64 编码图像

发布于 2025-01-05 01:33:59 字数 517 浏览 2 评论 0原文

我尝试使用 php 和 mySQL 将 BASE64 编码的图像嵌入到 XML 文件中,但没有成功。 图像作为 BLOB 字段存储在 mySQL 中。 以下是我正在开发的 php 脚本的一部分。 图像 blob 字段名为“filebin”。 我不断收到编码错误(即使我将字段设置为 CDATA)!

请帮忙!

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; 
$xml_output .= "<products>\n"; 

foreach($result as $row){ 
    $xml_output .= "\t<item>\n";
    $xml_output .= "\t\t<id>". $row['id'] . "</id>\n";
    $xml_output .= "\t\t<filebin>".$row['filebin']."</filebin>\n";
}

I'm trying with no success to embed BASE64 encoded images in an XML file with php and mySQL.
The images are stored as BLOB fields in mySQL.
The following is a part of the php script I'm developing.
The image blob field is named "filebin".
I keep on getting encoding errors (even if I set the field as CDATA)!

Please help!

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; 
$xml_output .= "<products>\n"; 

foreach($result as $row){ 
    $xml_output .= "\t<item>\n";
    $xml_output .= "\t\t<id>". $row['id'] . "</id>\n";
    $xml_output .= "\t\t<filebin>".$row['filebin']."</filebin>\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2025-01-12 01:33:59

使用 DOM 的示例

$doc = new DOMDocument('1.0', 'UTF-8');
$products = $doc->createElement('products');
$doc->appendChild($products);
foreach ($result as $row) {
    $item = $doc->createElement('item');
    $product->appendChild($item);

    $id = $doc->createElement('id', $row['id']);

    // assuming the BLOB data is binary and not already base64 encoded
    $filebin = $doc->createElement('filebin', base64_encode($row['filebin']));

    $item->appendChild($id);
    $item->appendChild($filebin);
}

$xml_output = $doc->saveXML();

Example using DOM

$doc = new DOMDocument('1.0', 'UTF-8');
$products = $doc->createElement('products');
$doc->appendChild($products);
foreach ($result as $row) {
    $item = $doc->createElement('item');
    $product->appendChild($item);

    $id = $doc->createElement('id', $row['id']);

    // assuming the BLOB data is binary and not already base64 encoded
    $filebin = $doc->createElement('filebin', base64_encode($row['filebin']));

    $item->appendChild($id);
    $item->appendChild($filebin);
}

$xml_output = $doc->saveXML();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文