2012-12-03 27 views
3

我有兩個文件夾圖像和帶照片的大圖像。通過PHP從目錄中的文件生成XML

我想生成具有兩個屬性像這樣的XML文件:

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

我有這樣的事情:

<?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!!!"; 
?> 

而現在的問題是如何與路徑添加第二個屬性來自「bigimages」目錄的圖像。

回答

1

這應該做到這一點:

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

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

參見:basename()

+0

它添加一些這樣想 <圖像源= 「圖像/ image1.jpg」 收藏夾= 「images1 /」/> 我想要有lightbox =「images1/image1.jpg 任何想法和如何做到字母從a到z ?? – user1874062

+0

好的問題解決:) – user1874062