php遍历文件夹(php遍历文件夹和文件名称)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈php遍历文件夹,以及php遍历文件夹和文件名称对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
- 1、php 循环遍历文件夹下面的所有目录及文件并且每个文件都写入一句话
- 2、PHP如何遍历指定文件夹,获取所有文件列表并生成下载链接??
- 3、求PHP遍历文件夹代码
- 4、php写一个函数,能够遍历一个文件夹下的所有文件和子文件夹
php 循环遍历文件夹下面的所有目录及文件并且每个文件都写入一句话
/****************************
* 获取目录下的所有文件
* [$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+');
}
}

PHP如何遍历指定文件夹,获取所有文件列表并生成下载链接??
试编写代码如下:
?php
$dir="D:/WWW/ftp"; //指定的路径
$sitepath = '';
//遍历文件夹下所有文件
if (false != ($handle = opendir ( $dir ))) {
echo "$dir 目录下的文件列表:BR/";
$i = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." $file != ".." !is_dir($dir.'/'.$file)) {
echo 'a href="' . $sitepath . $file . '"'.$file. '/abr/';
}
}
//关闭句柄
closedir($handle);
}
?
代码中需要提示的是:
如果是运行于互联网上,需要考虑文件的访问安全性。
运行截图:
求PHP遍历文件夹代码
1楼的弱爆了..
现在都玩php5了..用scandir函数最方便.
?php
$dir = "."; //当前目录
list_file($dir);
function list_file($dir){
$list = scandir($dir); // 得到该文件下的所有文件和文件夹
foreach($list as $file){//遍历
$file_location=$dir."/".$file;//生成路径
if(is_dir($file_location) $file!="." $file!=".."){ //判断是不是文件夹
echo "------------------------sign in $file_location------------------";
list_file($file_location); //继续遍历
}
echo "br/";
}
}
?
php写一个函数,能够遍历一个文件夹下的所有文件和子文件夹
最近刚写的,可以遍历指定目录下的所有文件、文件夹、特定后缀的文件:
/**
* 遍历目录
* @param string $dir 绝对/相对路径
* @param string $filter 默认*返回所有文件及文件夹,*.php仅返回php文件,如果$patten为GLOB_BRACE可实现多文件筛选,如*.{php,html},返回php和html文件
* @param const $patten 默认GLOB_BRACE,可选:GLOB_ONLYDIR,更多参数请参考手册
* @param string/bool $nocache 防止本次调用的结果缓存上次的结果,如果一个脚本仅调用一次本函数,则不用管,否则得设个值
* @return array
*/
function globdir($dir, $filter = '*', $patten = GLOB_BRACE, $nocache = null) {
static $file_arr = array ();
isset($nocache) $file_arr = array ();
if (!is_dir($dir)) return;
if ($patten == GLOB_ONLYDIR) {
$code = 'if (is_dir($file)) {$file_arr[] = $file;globdir($file, "*", GLOB_ONLYDIR);}';
} else {
$code = 'is_file($file) ? $file_arr[] = $file : globdir($file,"' . $filter . '",' . $patten . ');';
}
array_walk(glob("{$dir}/{$filter}", $patten), create_function('$file, $k, $file_arr', $code), $file_arr);
if ($filter != '*') {
array_walk(glob("{$dir}/*", GLOB_ONLYDIR), create_function('$dir,$k,$param', 'list($filter, $patten) = explode("|", $param);globdir($dir, $filter, $patten);'), "{$filter}|{$patten}");
}
return $file_arr;
}
php遍历文件夹的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php遍历文件夹和文件名称、php遍历文件夹的信息别忘了在本站进行查找喔。
