3

I've got two folders images and big images with photos.

I want to generate an XML file with two attributes like this :

<images>
    <image source="images/image1" lightbox="bigimages/image1" />
    .....
</images>

I have something like this :

<?php

    // enter the path to the folder
    $path = "images/";


    // opendir
    $dir_handle = @opendir($path) or die("Unable to open $path");

    // table photos
    $filetypes = array("jpg", "png");

    // forming xml
    $doc = new DomDocument('1.0');


    $doc->formatOutput = true;

    // forming images

    $root = $doc->createElement('images');
    $root = $doc->appendChild($root);

    while ($file = readdir($dir_handle)) {


        $file = rawurlencode($file);

        $split = explode(".", $file);
        $ext = strtolower($split[count($split)-1]);


        if (in_array($ext, $filetypes)) {

            // additional image
            $item = $doc->createElement("image");
            $item = $root->appendChild($item);


            $file = $path.$file;

            // adding an attribute source
            $item->setAttribute('source', $file);


        }


    }




    // closure
    closedir($dir_handle);

    // Save to XML
    $doc->save("plik.xml");
    echo "plik xml wygenerowany poprawnie!!!";
?>

And now the question is how to add second attribute with path to images from "bigimages" directory.

1 Answer 1

1

This should do it:

$big_image = 'bigimages/' . basename($file);

if (file_exists($big_image)) {
    $item->setAttribute('source', $big_image);
}

See also: basename()

Sign up to request clarification or add additional context in comments.

1 Comment

it add some think like this <image source="images/image1.jpg" lightbox="images1/"/> I want to have lightbox="images1/image1.jpg Any ideas and how to do it alphabetic from a to z??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.