4

My problem is a PHP & XML problem, i work with a friend on a image upload api, and we have a problem by creating the XML string.

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();

upload.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");
}

the problem is if we connect on the XML all is okay but if we upload a image, we become this error: error on line 1 at column 6: XML declaration allowed only at the start of the document

and the output like this:

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

why is a ? in the code !!

Sam Sussman
  • 1,005
  • 8
  • 27
Black
  • 43
  • 3
  • 1
    Do you have any output before your opening ` – Charles Dec 18 '12 at 21:35
  • thanks it work. I look in the HEX code the upload.php and found it :p – Black Dec 19 '12 at 10:26
  • 2
    Maybe you want to mark this question as "answered" then. – fbitterlich Dec 21 '12 at 14:05
  • How i can do this ? I`m new on Stackoverflow. i can only Share | Edit | Delete or Flag but i need the close button =( http://stackoverflow.com/faq#close – Black Dec 21 '12 at 15:25
  • I left an answer so you can accept it. If you dislike that, I suggest you write an answer your own (also accepted). @Charles: You might want to create an answer out of your comment, too. --- in the end please mark this question as answered, so it disappears from the unanswered listing. Thank you! – hakre Jan 01 '13 at 22:18

1 Answers1

0

It is likely that you have some character before the beginning of the XML document.

That is normally causing the error.

Open the script in a hex viewer, this normally shows the often otherwise in an editor invisible characters. Remove these, e.g. save the PHP files without BOM (you should always do that anyway).

hakre
  • 193,403
  • 52
  • 435
  • 836