我有两个文件夹图像和带照片的大图像。
我想生成一个具有两个属性的 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”目录中图像的路径。
这应该可以做到:
$big_image = 'bigimages/' . basename($file);
if (file_exists($big_image)) {
$item->setAttribute('source', $big_image);
}
也可以看看:基本名称() http://www.php.net/basename
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)