通过PHP从目录中的文件生成XML [英] Generate XML from file in directory via PHP
本文介绍了通过PHP从目录中的文件生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想生成一个带有两个属性的XML文件:
< images>
< image source =images / image1lightbox =bigimages / image1/>
.....
< / images>
我有这样的东西:
<?php
//输入文件夹的路径
$ path =images /;
// opendir
$ dir_handle = @opendir($ path)or die(Unable to open $ path);
//表照片
$ filetypes = array(jpg,png);
//形成xml
$ doc = new DomDocument('1.0');
$ doc-> formatOutput = true;
//形成图像
$ 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)){
//附加图像
$ item = $ doc-> createElement 图片);
$ item = $ root-> appendChild($ item);
$ file = $ path。$ file;
//添加一个属性源
$ item-> setAttribute('source',$ file);
}
}
//关闭
closedir($ dir_handle);
//保存到XML
$ doc-> save(plik.xml);
echoplik xml wygenerowany poprawnie !!!;
?>
现在的问题是如何添加第二个属性与路径到bigimages目录的图像。 / p>
解决方案
应该这样做:
$ big_image ='bigimages /'。基名($文件);
if(file_exists($ big_image)){
$ item-> setAttribute('source',$ big_image);
}
另请参见: basename()
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.
解决方案
This should do it:
$big_image = 'bigimages/' . basename($file);
if (file_exists($big_image)) {
$item->setAttribute('source', $big_image);
}
See also: basename()
这篇关于通过PHP从目录中的文件生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文