導航:首頁 > 編程語言 > php獲取目錄所有文件名

php獲取目錄所有文件名

發布時間:2024-04-09 05:07:53

php獲取文件夾下所有文件名

function dir_file_handle($dir,$dir_handle,$file_handle,&$array=NULL) {
$array = array();
function circle($dir,$dir_handle,$file_handle,&$array){
$handle=opendir($dir);
while(($file=readdir($handle))!==false){
if($file!="." && $file!=".."){
if(is_dir("$dir/$file")){
//文件夾操作
eval($dir_handle);
circle("$dir/$file",$dir_handle,$file_handle,&$array);
}else{
//文件操作
eval($file_handle);
}
}
}
}
@circle($dir,$dir_handle,$file_handle,$array);
}
function list_dir_file($array) {
$indent = 0;
static $preindent = 0;
while (list($key,$value) = each($array)) {
$indent = count(explode('/', $key));
if ($indent == 1) {
$indent = $preindent;
}else {
$preindent = $indent;
}
$dirname = substr(strrchr($key, '/'), 1);
if (is_array($value)) {
echo str_repeat("", $indent)."|-$dirname<br>";
list_dir_file($value);
}else {
echo str_repeat("", $indent*2)."$value<br>";
}
}
}

dir_file_handle(
'./01',
'
if(is_null($array["$dir"])) {
$array["$dir"] = array();
}
if(is_null($array["$dir/$file"])) {
$array["$dir/$file"] = array();
}
',
'array_push($array["$dir"],$file);',
$array
);
list_dir_file($array);

Ⅱ PHP如何獲取文件夾下所以子文件夾的名稱及子夾所以圖片

先寫個函數來遍歷文件夾下的所有子文件:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != ".." && $file != "." ) {
if ( is_dir($dir . "/" . $file) ) {
$files[$file] = scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}

若你想看圖片什麼的,可以給已遍歷出來的文件名加個鏈接,鏈接地址要正確哦!這樣當你點擊後就可以顯示了,具體你可以自己實現一下

Ⅲ php列出目錄所有文件名

php.ini
<?php
/*
Start Web Settings.
*/
define("TITLE","網站標題");
//Another method to define a constant.
//const TITLE = "網站標題";
//Start Web Settings End.
/*
Start MySQL Settings.
*/
$MySQL_HOST = "localhost";
$MySQL_PORT = "3306";
$MySQL_USER = "root";
$MySQL_PASSWORD = "";
$MySQL_DBNAME = "test";
//$MySQL = new pdo("mysql:dbname=".$MySQL_DBNAME.";host=".$MySQL_HOST,$MySQL_USER,$MySQL_PASSWORD);
//Start MySQL Settings End.
/*
Start Classes Settings.
*/
class Show_Files {
//獲取絕對路徑,未完善
function tr_directory($path_ = "/",$pre_path = "") {
if(strlen($pre_path) == 0){
$pre_path = getcwd();
}
if(is_dir($pre_path)){
if(is_dir($path_)){
$path = $path_;
}
else{
$path = $path_;
}
}
return $pre_path.$path_;
closedir($handle);
}
//獲取路徑中的所有文件名稱,可排除部分類型的文件
function get_files($path = "./",$nofile = array()){
$source = scandir($path);
$files = array();
$out_ = array();
foreach($source as $file){
if(is_file($file)){
$files[] .= $file;
}
}
$out_ = preg_replace($nofile,"",$files);
$out = array();
foreach($out_ as $out_tmp){
if(strlen($out_tmp)>0){
$out[] .= $out_tmp;
}
}
return $out;
closedir($handle);
}
//獲取路徑中的所有目錄名稱
function get_dirs($path = "./",$nodir = array()){
$source = scandir($path);
$dirs = array();
foreach($source as $dir){
if(is_dir($dir)){
if(count($nodir) > 0){
foreach($nodir as $no_dir){
if(!preg_match($no_dir,$dir)){
$dirs[] .= $dir;
}
}
}
else{
$dirs[] .= $dir;
}
}
}
$dirs = preg_replace($nodir,"",$dirs);
$dirs_out = array();
foreach($dirs as $dir_tmp){
if(strlen($dir_tmp)>0){
$dirs_out[] .= $dir_tmp;
}
}
return $dirs_out;
closedir($handle);
}
function get_alias($files=array("/etc/apache2/apache2.conf","/etc/apache2/httpd.conf","/etc/apache2/sites-enabled/000-default","/etc/apache2/sites-enabled/000-default")){
$alias = array('name'=>array(),'dir'=>array(),'file'=>array());
foreach($files as $alias_file){
$res = shell_exec("cat ".$alias_file." | grep Alias");
$res = explode("\n",$res);
//去掉行首空白和tab符
foreach($res as $res){
if(strlen($res)>0){
$res = trim($res);
$res = explode(" ",$res);
$alias['file'][] .= $alias_file;
$alias['name'][] .= $res[1];
$alias['dir'][] .= $res[2];
}
}
}
return $alias;
}
}
//Start Classes Settings End.
?>
index.php
<?php
include_once("./ini.php");
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en" xml:lang="en">
<head>
<title><?php echo TITLE." - 首頁"; ?></title>
<meta http-equiv="Content-Type" content="txt/html; charset=utf-8" />
<style type="text/css">
html{
background: #ddd;
}
body {
margin: 1em 10%;
padding: 1em 3em;
font: 80%/1.4 tahoma, arial, helvetica, lucida sans, sans-serif;
border: 1px solid #999;
background: #eee;
position: relative;
}
h2 {
margin: 0.8em 0 0 0;
}
</style>
<link rel="shortcut icon" href="index.php?img=favicon" type="./image/ico" />
</head>
<body>
<?php

?>
<div>
<h2 align="center">站點導航</h2>
</div>
<div>
<?php
$f = new Show_Files;
//列出可訪問目錄及該目錄下一級目錄和文件
$dir = $f -> tr_directory("/");
echo "<pre>";
$dirs = $f -> get_dirs($dir,array("/^\./"));
echo "<p><span style=\"font-size:14px;font-weight:bold\">目錄</span>(已略去隱藏目錄)</p>";
foreach($dirs as $dirs){
echo "<p><a href=\"".$dirs."\">".$dirs."</a></p>";
}
//列出Alias配置的列表
echo "<p><span style=\"font-size:14px;font-weight:bold\">Alias</span></p>";
$alias_file = array("/etc/apache2/apache2.conf","/etc/apache2/httpd.conf","/etc/apache2/sites-enabled/000-default");
$alias = $f -> get_alias($alias_file);
foreach($alias['name'] as $alia){
echo "<a href=\"".$alia."\">".$alia."</a>\n";
}
//列出可訪問文件
echo "<p><span style=\"font-size:14px;font-weight:bold\">文件</span>(已略去隱藏文件、備份文件、配置文件)</p>";
$files = $f -> get_files($dir,array("/(.*)\.bak$/","/(.*)\~$/","/(.*)ini.php$/","/^\.(.*)/"));
foreach($files as $files){
echo "<a href=\"".$files."\">".$files."</a>\n";
}
echo "</pre>";
?>
</div>
</body>
</html>

自己寫的提供出來看看,感興趣的朋友可以一起討論:[email protected]

Ⅳ php列出目錄下所有文件(包括子目錄)

<?php

/**
*Goofy2011-11-30
*getDir()去文件夾列表,getFile()去對應文件夾下面的文件列表,二者的區別在於判斷有沒有「.」後綴的文件,其他都一樣
*/

//獲取文件目錄列表,該方法返回數組
functiongetDir($dir){
$dirArray[]=NULL;
if(false!=($handle=opendir($dir))){
$i=0;
while(false!==($file=readdir($handle))){
//去掉"「.」、「..」以及帶「.xxx」後綴的文件
if($file!="."&&$file!=".."&&!strpos($file,".")){
$dirArray[$i]=$file;
$i++;
}
}
//關閉句柄
closedir($handle);
}
return$dirArray;
}

//獲取文件列表
functiongetFile($dir){
$fileArray[]=NULL;
if(false!=($handle=opendir($dir))){
$i=0;
while(false!==($file=readdir($handle))){
//去掉"「.」、「..」以及帶「.xxx」後綴的文件
if($file!="."&&$file!=".."&&strpos($file,".")){
$fileArray[$i]="./imageroot/current/".$file;
if($i==100){
break;
}
$i++;
}
}
//關閉句柄
closedir($handle);
}
return$fileArray;
}

//調用方法getDir("./dir")……
?>

Ⅳ PHP 如何 得到文件名(不要後綴名)

方法如下:

$dh = opendir($dirname);

$n = 0;

while ($file=readdir($dh)){

// $file為目錄下所有文件和文件夾,

// 需要注意默認有 .(本目錄)和..(上級目錄)這兩個特殊文件夾

if (is_file($file)){

//只處理文件

if (!strpos($file,'.')){

// 無後綴名的文件

}

$n++; // 計數

}

}

Ⅵ PHP如何獲取文件夾的文件名稱

當前目錄的路徑?

__DIR__或dirname(__FILE__)

preg_match('#([^/]+)$#',str_replace('\','/',__DIR__),$match);

var_mp($match[1]);

閱讀全文

與php獲取目錄所有文件名相關的資料

熱點內容
背著媽媽在樓道和男朋友韓劇 瀏覽:292
用php開發購物網站 瀏覽:965
歐美四級懷舊老片 瀏覽:979
比較黃的台灣電影 瀏覽:751
圖片批量在線壓縮 瀏覽:705
比較黃的穿越小說 瀏覽:250
電影人狗叫 瀏覽:507
取消壓縮管理的公司 瀏覽:116
打真軍的電影豆瓣 瀏覽:168
特斯拉app超級充電樁如何使用 瀏覽:422
男人懷孕電影袋鼠男人 瀏覽:507
如何刪去應用加密 瀏覽:468
服從命令聽指揮陌千依小說 瀏覽:761
《赤足驚魂》電影介紹 瀏覽:359
ic卡內數據如何傳到伺服器 瀏覽:21
安卓系統如何更改托盤 瀏覽:403
許志安演的電影有哪些 瀏覽:824
odbc文件夾是什麼 瀏覽:391
男主用鐵鏈囚禁女主的小說 瀏覽:914
sift演算法harris演算法 瀏覽:241