foreach (scandir($folder) AS $value) {
if ($value == '.' OR $value == '..') continue
echo '文件名 : ' . $value . "\n"
}
/***************************** 获取目录下的所有文件
* [$dir] 文件夹路径
****************************/
function deepScanDir($dir) {
$fileArr = array ()
$dirArr = array ()
$dir = rtrim($dir, '//')
if (is_dir($dir)) {
$dirHandle = opendir($dir)
while (false !== ($fileName = readdir($dirHandle))) {
$subFile = $dir . DIRECTORY_SEPARATOR . $fileName
if (is_file($subFile)) {
$fileArr[] = $subFile
}
elseif (is_dir($subFile) && str_replace('.', '', $fileName) != '') {
$dirArr[] = $subFile
$arr = deepScanDir($subFile)
$dirArr = array_merge($dirArr, $arr['dir'])
$fileArr = array_merge($fileArr, $arr['file'])
}
}
closedir($dirHandle)
}
return array (
'dir' => $dirArr,
'file' => $fileArr
)
}
/****************************
* 将内容写入文件
* [$filename] 文件路径
* [$contents] 文件内容
* [$type] 读写类型
****************************/
function writeFileContents($filename, $contents, $type='a') {
if (!($fd = fopen($filename, $type)))
return FALSE
if (!fwrite($fd, $contents."\n")) {
fclose($fd)
return FALSE
}
fclose($fd)
return true
}
#示例:
$dir = "/usr/local/php/test/"
$dirFiles = deepScanDir($dir)
if(!empty($dirFiles['file'])){
foreach($dirFiles['file'] as $file){
writeFileContents($file, "Hello", $type='a+')
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)