fill $_FILES argument from XML parser

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

Threaded View
I've a function asking for a $_FILES argument:

function insertarticle($particle,......,$pUserID,$pOptions,$files){
in this function I do add the record and then save the files in a
subdirectory specific on some datas.

This function is used by my "insert" form, letting the user to select images
files to upload with <FILE> html tag.
The files to upload are then passed to the $files like this:

for mantainance purpose, I avoid as much as possible to have the same code
in 2 different pages. Also, using actually this function to add a record in
the database (quite complex code) and saving files from a form, and using
the same function to add records from an XML parser, I can't send images
files to the function.

My parser can save the images files on the server's directory, but how to
pass $files parameters in order to save those files ? It's there any way to
take the string containing the base64 image code (I do actually save it
locally) and put it in a $_FILES array for passing it to the function ?

thanks for help.


Re: fill $_FILES argument from XML parser

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

You will have to convert each $_FILES array to a format that can be parsed
by the XML parsing function.

When the code of the XML parsing function equals the one in your earlier
post, the expected array probably looks like this:

Array (
       [NUM] => 2,
       [PICTURES] => Array(
                "base64 encoded data"
                "base64 encoded data"

The $_FILES array would contain something like the following:

    [uploadfieldname] => Array
            [name] => test.png
            [type] => image/x-png
            [tmp_name] => /tmp/php3D.tmp
            [error] => 0
            [size] => 30230


Now, to convert the arrays, you could use the following code:

    if (isset($_FILES)) {
         $data = array();
         foreach ($_FILES as $uploaded) {
              if (!$uploaded["error"]) {
                   $pic = array(

                  if (!isset($data["PICTURES"])) {
                        $data["PICTURES"] = array($pic);
                  } else {
                        $data["PICTURES"][] = $pic;

                  if (!isset($data["NUM"])) {
                        $data["NUM"] = 1;
                  } else {

After this you can call the XML parser function with $data as the argument.
However, as you will have noticed, the image data get base64_encoded.

It would be better to introduce a flag in the PICTURES array of $data to
indicate that the data is base64 encoded...


Site Timeline