pictures from XML file.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I've been able to parse the XML files used to update my database.

Now, I receive image in XML files, like this:

<PIC NR="1"><![CDATA[/9j/4AAQSkZJ.....=]]></PIC>
<PIC NR="2"><![CDATA[/9j/4AAQSk.............></PIC>
<PIC NR="3"><![CDATA[/9j/4AAQSk.............></PIC>

How to save those 3 images in 3 jpg files ???



Re: pictures from XML file.

On Wed, 15 Sep 2004 22:28:45 +0200, "Bob Bedford"

Quoted text here. Click to load it

 Since you've said you've got the parsing sorted, that must just leave decoding
and saving.

 Looks like it's base64 encoded, so:

< Space: disk usage analysis tool

Re: pictures from XML file.

Quoted text here. Click to load it

Here is the code, but probably the problem is somewhere else:
          while(list($k,$v) = each($datas["PICTURES"])){
            while(list($k1,$v1) = each($datas["PICTURES"][$k])){
             if($k1 == 'PIC'){
               echo(base64_decode($v1)."<br><br>"); //print to screen
               if($imgfile =
               echo('error creating output file');
              $num = $v1;

Parsing: got the code on
  while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
    die(sprintf("XML error: %s at line %d",
    rename($XMLPath.$file, $XMLPath.$failpath.$file); //move file in error

the XML structure
<PIC NR="1"><![CDATA[/9j/4AAQSkZJRg...............

if I try to print on the screen with "echo(base64_decode($v1)."<br><br>");"
I get line of different size and the created files are 1ko ! They should be
bigger ! Any idea ??? any string limit, a problem in the parser ???



Re: pictures from XML file.

*** Bob Bedford escribió/wrote (Thu, 16 Sep 2004 00:00:19 +0200):
Quoted text here. Click to load it

Note that fread() uses a 4096 bytes buffer. That means that an image will
probably be split in more than one while() looping. Just make sure your XML
parser takes this case into account.

-+ Álvaro G. Vicario - Burgos, Spain
+- (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions

Re: pictures from XML file.

Bob Bedford wrote:
Quoted text here. Click to load it

You said that you are able to parse the XML files, so I'm not going to
suggest how to do this.

There are someways to write the data to a file, one of the most simple ones
is with:

This function is binary-safe. Before file creation, you should decode the
data when it's encoded. When the data isn't encoded, I advise you to encode
it before addition to the XML file. Otherwise, some binary data can get

A common way of encoding is base64, for which you can apply base64_decode()
to decode the data and base64_encode() for encoding.


Site Timeline