[英]How to exclude certain file types with PHP readdir?
I am using a php scan directory script that will scan the contents of a directory and then populate the page with links to the directory contents. 我正在使用一个php扫描目录脚本,它将扫描目录的内容,然后使用指向目录内容的链接填充页面。
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("<a href=\"".$file."\">".$file."</a><br />\n");
}
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?
I am wondering how I can exclude certain files or file types like test.xml, durka.xslt or .html from showing up on the populated page. 我想知道如何排除某些文件或文件类型,如test.xml,durka.xslt或.html显示在填充页面上。 I have some code but not sure how to integrate it.
我有一些代码,但不知道如何集成它。 Any help would be very appreciated...
任何帮助将非常感谢...
?php
if ($handle = opendir(‘.’)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “..”
&& $file != “NOT-TO-BE-IN-1.php”
&& $file != “NOT-TO-BE-IN-2.html”
&& $file != “NOT-TO-BE-IN-3.jpg”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
)
<?php
// These files will be ignored
$excludedFiles = array (
'excludeMe.file',
'excludeMeAs.well'
);
// These file extensions will be ignored
$excludedExtensions = array (
'html',
'htm',
'php'
);
// Make sure we ignore . and ..
$excludedFiles = array_merge($excludedFiles,array('.','..'));
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Loop through directory
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
$extn = explode('.',$file);
$extn = array_pop($extn);
// Only echo links for files that don't match our rules
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)) {
$count++;
print("<a href=\"".$file."\">".$file."</a><br />\n");
}
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?>
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".."
&& $file != "NOT-TO-BE-IN-1.php"
&& $file != "NOT-TO-BE-IN-2.html"
&& $file != "NOT-TO-BE-IN-3.jpg"
&& substr($file,-strlen(".html")) != ".html" //if you don't want to include .html files, for instance
&& substr($file,-strlen(".js")) != ".js" //if you don't want to include .js files, for instance
&& $file != ""
) {$count++;
print("<a href=\"".$file."\">".$file."</a><br />\n");
}
}
echo '<br /><br /><a href="..">Return</a>';
closedir($handle);
}
?>
Other way: 另一种方式:
$excludeExtensions = array(
'php',
'html',
'jpg'
);
if ($file != "." && $file != ".." && !in_array(pathinfo($file, PATHINFO_EXTENSION), $excludeExtensions))
EDIT: again I was too late:) 编辑:我太晚了:)
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".."
&& $file != "NOT-TO-BE-IN-1.php"
&& $file != "NOT-TO-BE-IN-2.html"
&& $file != "NOT-TO-BE-IN-3.jpg"
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
)
echo file_get_contents($file);
will show contents of all pages which haven't name 将显示所有没有名字的页面的内容
you can restrict certain filepath with 你可以限制某些文件路径
if (!preg_match('/(php|html|jpg)/', $file))
you can also just use glob
: 你也可以使用
glob
:
foreach (glob("*.{php|html|jpg}",GLOB_BRACE) as $file) {
echo file_get_contents($file);
}
see http://php.net/manual/en/function.glob.php for more info 有关详细信息,请参阅http://php.net/manual/en/function.glob.php
Try something like: 尝试类似的东西:
$excluded_files = array('test.xslt');
$excluded_ext = array('html');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." &&
!in_array($file, $excluded_files) &&
!in_array(pathinfo($file, PATHINFO_EXTENSION), $excluded_ext))
{
// do stuff
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.