【问题标题】:Image Upload API XML (declaration allowed only at the start of the document)Image Upload API XML(仅在文档开头允许声明)
【发布时间】:2012-12-06 02:10:00
【问题描述】:

我的问题是 PHP 和 XML 问题,我和朋友一起处理图片上传 api,我们在创建 XML 字符串时遇到了问题。

image.php:

$simage = new SimpleImage();
    $xml = new SimpleXMLElement('<bb-upload></bb-upload>');
    if($key == get_key()){
        require 'upload.php';
    }
    else{
        $xml->addChild('error', "102 Fehlerhafter Zugriffs Key");
    }
    header("Content-type:text/xml;charset=utf-8");
    echo $xml->asXML();

上传.php:

if ($_FILES["image"]["error"] > 0) {
$xml->addChild('error', "101 Bilder fehler: " . $_FILES["image"]["error"]);
}
else{
  $data = array(
        'tmp'   => $_FILES["image"]["tmp_name"],
        'save'  => time().'-'.rand(1, 1000).'.jpg',
    );

    $filename = $data['save'];

    $simage->load($data['tmp']);
    $simage->resize(350,300);
    $simage->save("../data/$filename");

    $xml->addChild('error', "0");
    $xml->addChild('src', "http://*******.de/bb-image/data/$filename");
}

问题是如果我们在 XML 上连接一切正常,但如果我们上传图片,我们就会变成 error: error on line 1 at column 6: XML declaration allowed only at the start of the document

输出如下:

?<?xml version="1.0"?>
<bb-upload><error>0</error><src>http://*****.de/bb-image/data/1355858033-712.jpg</src>     </bb-upload>

为什么是 ?在代码中!!

【问题讨论】:

  • 你在打开&lt;?php标签之前有任何输出吗?也许是sneaky UTF-8 BOM
  • 感谢它的工作。我查看了upload.php的十六进制代码并找到了它:p
  • 也许您想将此问题标记为“已回答”。
  • 我该怎么做?我是 Stackoverflow 的新手。我只能分享 |编辑 |删除或标记,但我需要关闭按钮 =( stackoverflow.com/faq#close
  • 我留下了答案,以便您接受。如果您不喜欢这样,我建议您自己写一个答案(也接受)。 @Charles:您可能也想根据您的评论创建一个答案。 --- 最后请将此问题标记为已回答,以便它从未回答的列表中消失。谢谢!

标签: php xml image upload


【解决方案1】:

您可能在 XML 文档的开头之前有一些字符。

这通常会导致错误。

在十六进制查看器中打开脚本,这通常会在编辑器中显示通常不可见的字符。删除这些,例如保存没有 BOM 的 PHP 文件(无论如何你都应该这样做)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多