<?php
// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();
if ($dhandle) {
// loop through all of the files
while (false !== ($fname = readdir($dhandle))) {
if (($fname != 'other') && ($fname != 'dd') && ($fname != 'index.htm') && ($fname != 'torcache.php')&& ($fname != 'error_log') &&
($fname != basename($_SERVER['PHP_SELF']))) {
// store the filename
$files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
}
}
// close the directory
closedir($dhandle);
}我想要做的是,如果文件以'other‘或'dd’开头,那么不要在循环$files中包括它;除了在!=中命名整个文件名之外,我可以做些什么来排除这些文件?
发布于 2012-06-16 02:36:32
把这个加到你的支票上:
(substr($fname, 0, 5) != 'other') && (substr($fname, 0, 2) != 'dd')参见PHP substr。它接受一个字符串并返回一个子字符串,该子字符串从给定的第一个数字开始(0表示字符串的开始),长度由第二个数字指定(5表示“其他”,2表示"dd")。
因此,您的完整语句将是:
if (
(substr($fname, 0, 5) != 'other') &&
(substr($fname, 0, 2) != 'dd') &&
($fname != 'index.htm') &&
($fname != 'torcache.php') &&
($fname != 'error_log') &&
($fname != basename($_SERVER['PHP_SELF']))
) { ... }https://stackoverflow.com/questions/11056333
复制相似问题