我正在尝试创建一个允许用户选择复选框的表单,当他们提交时,它将在web目录中搜索他们检查的特定文件类型(jpg,gif,html等)。我已完成表单并将类型的变量传递给数组,我有其余的表单来压缩文件并将其流式传输以供下载。我只是坚持如何指定文件类型。这是我开始的,以确保它可以实际拉取文件名并将它们放在一个变量,它的工作原理:
$rootpath = '.';
$files = $_POST['file_types'];
$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootpath)
);
foreach($fileinfos as $pathname => $fileinfo) {
if (!$fileinfo->isFile()) continue;{
$files[] .= substr($pathname, 2);
}
}
我应该使用什么来提取仅在以下vaiable中指定的文件类型?
$_POST['file_types']
编辑:
对于那些可能有所帮助的人来说,最终的解决方案是有效的:
$rootpath = "../";
foreach ($types as $k => $v)
{
$types_array[] .= $v;
}
//Run all function then run it through checker and pull out the files we want
$directory = new RecursiveDirectoryIterator($rootpath,RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);
$extensions = $types_array;
foreach ($iterator as $fileinfo) {
if (in_array($fileinfo->getExtension(), $extensions)) {
$files[] = substr($fileinfo->getPathname(), 2); /* Fill the array removing the "./" to make an echo of the file names easier to read for the end user */
}
}