国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP文件與目錄操作示例

2024-05-04 23:53:56
字體:
來源:轉載
供稿:網友

本文實例講述了PHP文件與目錄操作。分享給大家供大家參考,具體如下:

文件目錄相關函數

<?php// 輸出目錄中的文件function outputcurfiles ($allowedtypes, $thedir){//首先,我們確保目錄存在。if (is_dir ($thedir)){ //現在,我們使用scandir掃描目錄中的文件。 $scanarray = scandir ($thedir); //接著我們開始解析數組。 //scandir()用“.”和“..”統計文件導航列表 //因此作為文件,我們不應該列出他們。 for ($i = 0; $i < count ($scanarray); $i++){  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){   //現在,進行檢查,以確保這是一個文件,而不是一個目錄。   if (is_file ($thedir . "/" . $scanarray[$i])){    //現在,因為我們將允許客戶端編輯這個文件,    //我們必須檢查它是否是可讀和可寫。    if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){     //現在,我們檢查文件類型是否存在于允許的類型數組中.     $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);     if (in_array ($thepath['extension'], $allowedtypes)){      //如果文件符合規定,我們可以繼續輸出.      echo $scanarray[$i] . "<br />";     }    }   }  } }} else { echo "對不起,這個目錄不存在.";}}$allowedtypes = array ("txt","html");outputcurfiles ($allowedtypes, "testfolder");///////////////////////////////////////////////////function recurdir ($thedir) {  //First attempt to open the directory.  try {    if ($adir = opendir ($thedir)){      //掃描目錄。      while (false !== ($anitem = readdir ($adir))){        //不統計目錄中包含“.”或“..”的情況        if ($anitem != "." && $anitem != ".."){          //此時如果是一個目錄,則縮進一點          //再去遞歸          if (is_dir ($thedir . "/" . $anitem)){            ?><span style="font-weight: bold;" mce_style="font-weight: bold;"><?php echo $anitem; ?></span><?php            ?><div style="margin-left: 10px;" mce_style="margin-left:10px;"><?php            recurdir ($thedir . "/" . $anitem );            ?></div><?php          } elseif (is_file ($thedir . "/" . $anitem)){            //此時輸出文件.            echo $anitem . "<br />";          }        }      }    } else {      throw new exception ("Sorry, directory could not be openend.");    }  } catch (exception $e) {    echo $e->getmessage();  }}echo "<br />/////////////////////////////////////<br /><br />";recurdir("testfolder");//////////////////////////////////////////////////////////////////echo "<br />/////////////////////////////////////<br /><br />";function sortfilesbydate ($thedir){  //首先,需要確保目錄存在。  if (is_dir ($thedir)){    //接著,我們使用scandir掃描此目錄中的文件.    $scanarray = scandir ($thedir);    $finalarray = array();    //然后開始解析數組    //scandir()用“.”和“..”統計文件導航列表    //因此作為文件,我們不應該列出他們.    for ($i = 0; $i < count ($scanarray); $i++){      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){        //現在,我們檢查,以確保這是一個文件,而不是一個目錄.        if (is_file ($thedir . "/" . $scanarray[$i])){          //現在需要做的是循環數據到一個關聯數組.          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);        }      }    }    //至此,我們已經遍歷了整個數組,現在需要做的只是asort()它。    asort ($finalarray);    return ($finalarray);  } else {    echo "對不起,這個目錄不存在.";  }}//然后,我們將函數指向我們需要查看的目錄.$sortedarray = sortfilesbydate ("testfolder");//至此,就可以按照如下形式輸出:while ($element = each ($sortedarray)){  echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";}?>

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宜兰县| 孝感市| 库尔勒市| 凤冈县| 金乡县| 南雄市| 南开区| 故城县| 尤溪县| 如东县| 新竹县| 兴山县| 清河县| 方城县| 乃东县| 锡林浩特市| 启东市| 叙永县| 蒲城县| 五寨县| 怀来县| 锡林浩特市| 沅陵县| 梓潼县| 玉山县| 洛阳市| 凤城市| 云南省| 双峰县| 平谷区| 三穗县| 莱州市| 手游| 祥云县| 青阳县| 绵竹市| 濮阳县| 高唐县| 会同县| 瓦房店市| 东辽县|