『壹』 php讀取目錄下所有文件內容並顯示
<?php
function printFile($filepath)
{
//substr(string,start,length)函數返回字元串的一部分;start規定在字元串的何處開始 ;length規定要返回的字元串長度。默認是直到字元串的結尾。
//strripos(string,find,start)查找 "php" 在字元串中最後一次出現的位置; find為規定要查找的字元;start可選。規定開始搜索的位置
//讀取文件後綴名
//$filetype = substr ( $filename, strripos ( $filename, "." ) + 1 );
//判斷是不是以txt結尾並且是文件
#if ($filetype == "txt" && is_file ( $filepath . "/" . $filename ))
if ( is_file ( $filepath))
{
$filename=iconv("gb2312","utf-8",$filepath);
echo $filename."內容如下:"."<br/>";
$fp = fopen ( $filepath, "r" );//打開文件
#while (! feof ( $f )) //一直輸出直到文件結尾
$i = 1;
while ($i < 10)
{
$line = fgets ( $fp );
echo $line."<br/>";
$i = $i +1;
}
fclose($fp);
}
}
(此處空一行)
function readFileRecursive($filepath)
{
if (is_dir ( $filepath )) //判斷是不是目錄
{
$dirhandle = opendir ( $filepath );//打開文件夾的句柄
if ($dirhandle)
{
//判斷是不是有子文件或者文件夾
while ( ($filename = readdir ( $dirhandle ))!= false )
{
if ($filename == "." or $filename == "..")
{
//echo "目錄為「.」或「..」"."<br/>";
continue;
}
//判斷是否為目錄,如果為目錄遞歸調用函數,否則直接讀取列印文件
if(is_dir ($filepath . "/" . $filename ))
{
readFileRecursive($filepath . "/" . $filename);
}
else
{
//列印文件
printFile($filepath . "/" . $filename);
echo "<br/>";
}
}
closedir ( $dirhandle );
}
}
else
{
printFile($filepath . "/" . $filename);
return;
}
}
(此處空一行)
header("content-type:text/html;charset=utf-8");
#echo "Hello World"."<br/>";
$filepath = "C:/phpStudy/PHPTutorial/WWW/test/results"; //想要讀取的目錄
readFileRecursive($filepath )
?>
php還可以讀取文件夾下所有圖片,方法如下
hostdir=dirname(__FILE__).'/data/upload/admin/20170517/'; //要讀取的文件夾
(此處空一行)
$url = '/data/upload/admin/20170517/'; //圖片所存在的目錄
(此處空一行)
$filesnames = scandir($hostdir); //得到所有的文件
(此處空一行)
// print_r($filesnames);exit;
//獲取也就是掃描文件夾內的文件及文件夾名存入數組 $filesnames
(此處空一行)
$www = 'http://www.***.com/'; //域名
(此處空一行)
foreach ($filesnames as $name) {
$aurl= "<img width='100' height='100' src='".$www.$url.$name."' alt = '".$name."'>"; //圖片
echo $aurl . "<br/>"; //輸出他
『貳』 PHP怎麼讀取php所在文件夾下的圖片和mp3文件,並且顯示出來
<?php
$dir="./";//要獲取的目錄
echo"**********獲取目錄下所有文件和文件夾***********<hr/>";
//先判斷指定的路徑是不是一個文件夾
if(is_dir($dir)){
if($dh=opendir($dir)){
while(($file=readdir($dh))!=false){
if(getFileType($file)=="mp3"){
echo"mp3格式";
}
if(getFileType($file)=="jpg"||getFileType($file)=="png"||getFileType($file)=="gif"){
echo"圖片格式";
}
closedir($dh);
}
}
functiongetFileType($filename){
returnstrtolower(pathinfo($filename)['extension']);
}
?>
『叄』 php 獲取當前目錄所有文件夾名 及下級目錄文件夾名 求代碼詳解
把這個文件放到\wamp\www\ 這里,然後運行。
<?php
if (isset($_GET['dir'])){ //設置文件目錄
$basedir=$_GET['dir'];
}else{
$basedir = '.';
}
checkdir($basedir);
function checkdir($basedir)
{
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..'){
if (!is_dir($basedir."/".$file)) {
echo "filename: $basedir/$file <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
?>
[以下於為題無關]
嗎蛋,代碼前的空格都沒了,這不是我去掉的哦,是百X把空格全去了,有強迫症表示不能接受啊...........
『肆』 php怎麼獲取文件夾
給你一個文件夾,返回該文件夾下所有文件數量
<?php
//遞歸函數實現功能
function fileall($fname){
$sum = 0;
if(is_dir($fname)){
$dir = opendir($fname
);
while($name = readdir($dir)){
if($name != "." && $name != ".."){
$wzpath = $fname."/".$name;//將文件拼接成完整的路徑
if(is_file($wzpath)){
//如果是文件+1
$sum++;
}else{
//如果是文件夾調用本身函數查找所有文件
$sum += fileall($wzpath);
}
}
}
closedir($dir);
return $sum;
}else{
return 1;
}
}
函數調用:echo fileall("./bootstrap");
?>
『伍』 php如何讀取某目錄下的所有同類型文件
PHP的glob() 函數返回匹配指定模式的文件名或目錄。
該函數返回一個包含有匹配文件 / 目錄的數組。如果出錯返回 false。
語法
glob(pattern,flags)
參數
描述
file
必需。規定檢索模式。
size
可選。規定特殊的設定。
GLOB_MARK - 在每個返回的項目中加一個斜線
GLOB_NOSORT - 按照文件在目錄中出現的原始順序返回(不排序)
GLOB_NOCHECK - 如果沒有文件匹配則返回用於搜索的模式
GLOB_NOESCAPE - 反斜線不轉義元字元
GLOB_BRACE - 擴充 {a,b,c} 來匹配 'a','b' 或 'c'
GLOB_ONLYDIR - 僅返回與模式匹配的目錄項
GLOB_ERR - 停止並讀取錯誤信息(比如說不可讀的目錄),默認的情況下忽略所有錯誤
注釋:GLOB_ERR 是 PHP 5.1 添加的。
例子 1
<?php
$a=glob("*.txt");
print_r(count($a));
?>
『陸』 怎樣用php讀取一個目錄下所有文件的文件名
functionoutput_file($path){
if($handle=opendir(realpath($path))){
while(false!==($file=readdir($handle))){
if($file==='.'||$file==='..'){
continue;
}
$this_file=$path.'/'.$file;
if(is_file($this_file)){
echo$file.PHP_EOL;
}
if(is_dir($this_file)){
$this->output_file($this_file);
}
}
closedir($handle);
}else{
echo'Opendirectoryfailed!';
}
}
『柒』 php 怎樣讀取指定目錄下面的所有文件
functiontreeDirectory($dir)
{
$files=array();
$dirpath=realpath($dir);
$filenames=scandir($dir);
foreach($filenamesas$filename)
{
if($filename=='.'||$filename=='..')
{
continue;
}
$file=$dirpath.DIRECTORY_SEPARATOR.$filename;
if(is_dir($file))
{
$files=array_merge($files,self::treeDirectory($file));
}
else
{
$files[]=$file;
}
}
return$files;
}
『捌』 php如何獲取目錄下所有文件名
<?php
functionfilesinfo($path){
//檢查路徑合法性
if(!is_dir($path))returnfalse;//不合法
//用系統函數獲得文件名數組
$files=scandir($path);
//定義靜態變數記錄調用次數
static$count=-1;
//遍歷數組
foreach($filesas$file){
//如果是'.'或'..'文件,跳過當次循環
if($file=='.'||$file=='..')continue;
//將文件信息存入數組
$arr[]=iconv('gbk','utf-8',$file);
}
//每次調用,次數加1
$count+=1;
//根據調用次數返回第N個結果
return$arr[$count];
}
//第1次調用,./替換成你自己的目錄路徑
echofilesinfo('./').'</br>';
//第2次調用
echofilesinfo('./').'</br>';
//第3次調用
echofilesinfo('./').'</br>';
?>
『玖』 php中哪個函數可以取得某目錄下的所有文件名
沒有這樣的函數,只能自己實現,下面是我常用的,這個不僅會獲取到當前目錄下的所有文件,而且還會遍歷所有子文件夾下的文件。
<?php
functionread_all_dir($dir)
{
$result=array();
$handle=opendir($dir);
if($handle)
{
while(($file=readdir($handle))!==false)
{
if($file!='.'&&$file!='..')
{
$cur_path=$dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($cur_path))
{
$result['dir'][$cur_path]=read_all_dir($cur_path);
}
else
{
$result['file'][]=$cur_path;
}
}
}
closedir($handle);
}
return$result;
}
?>