fill $_FILES argument from XML parser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    fill $_FILES argument from XML parser

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

    function insertarticle($ particle,...... ,$pUserID,$pOpt ions,$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:
    insertarticle(1 884,......,4,$O ptions,$_FILES) ;

    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.

    Bob


  • Janwillem Borleffs

    #2
    Re: fill $_FILES argument from XML parser

    Bob Bedford wrote:[color=blue]
    > 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 ?
    >[/color]

    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 (
    Array(
    [NUM] => 2,
    [PICTURES] => Array(
    Array(
    "PIC",
    "base64 encoded data"
    ),
    Array(
    "PIC",
    "base64 encoded data"
    )
    )
    )
    )

    The $_FILES array would contain something like the following:

    Array
    (
    [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:

    <?php
    if (isset($_FILES) ) {
    $data = array();
    foreach ($_FILES as $uploaded) {
    if (!$uploaded["error"]) {
    $pic = array(
    "PIC",
    chunk_split(
    base64_encode(
    file_get_conten ts(
    $uploaded["tmp_name"]
    )
    )
    )
    );

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

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

    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...


    HTH;
    JW



    Comment

    Working...