所以我有这个代码,我正在使用它来使用xml数据表实时搜索我的所有图像。我现在遇到的问题是能够看到子文件夹名称集成到每个图像的文件名中。我的想法是,我将有一个文件夹中的图像和在该文件夹中的许多子文件夹中的更多图像。与其从子文件夹中取出所有这些图像,我宁愿在文件名中包含正确的子文件夹路径,这样所有子文件夹中的所有图像都可以包含在搜索中。
这是我当前的代码:
$path_to_image_dir = 'images'; // relative path to your image directory
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<images>
</images>
XML;
$xml_generator = new SimpleXMLElement($xml_string);
if ( $handle = opendir( $path_to_image_dir ) )
{
while (false !== ($file = readdir($handle)))
{
if ( is_file($path_to_image_dir.'/'.$file) )
{
list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
$image = $xml_generator->addChild('image');
$image->addChild('path', $path_to_image_dir.'/'.$file);
$image->addChild('height', $height);
$image->addChild('width', $width);
}
}
closedir($handle);
}
$file = fopen('data.xml','w');
fwrite($file, $xml_generator->asXML());
fclose($file);?>
我认为这行代码就是答案,但不知道如何或在哪里添加它,也不知道是否需要对代码进行任何更改。
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path_to_image_dir)) as
$file)
感谢您的帮助。提前感谢大家,干杯!
是的,您已经接近了。您可以使用SPL库来重复获取文件。示例:
$path_to_image_dir = 'images'; // relative path to your image directory
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<images>
</images>
XML;
$xml_generator = new SimpleXMLElement($xml_string);
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path_to_image_dir));
foreach($it as $path => $file) {
// you can use the `$path` key (which contains the path)
// or another way is $file->getPathname()
if($file->isDir()) continue; // skip folders
list( $width, $height ) = getimagesize($path);
$image = $xml_generator->addChild('image');
$image->addChild('path', $path);
$image->addChild('height', $height);
$image->addChild('width', $width);
}
$file = fopen('data.xml','w');
fwrite($file, $xml_generator->asXML());
fclose($file);