亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP文件與目錄操作示例

 更新時(shí)間:2016年12月24日 11:52:20   作者:trace332  
這篇文章主要介紹了PHP文件與目錄操作,涉及php針對文件與目錄的遍歷、判斷與排序相關(guān)操作技巧,注釋中備有較為詳細(xì)的說明,需要的朋友可以參考下

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

文件目錄相關(guān)函數(shù)

<?php
// 輸出目錄中的文件
function outputcurfiles ($allowedtypes, $thedir){
//首先,我們確保目錄存在。
if (is_dir ($thedir)){
 //現(xiàn)在,我們使用scandir掃描目錄中的文件。
 $scanarray = scandir ($thedir);
 //接著我們開始解析數(shù)組。
 //scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
 //因此作為文件,我們不應(yīng)該列出他們。
 for ($i = 0; $i < count ($scanarray); $i++){
  if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
   //現(xiàn)在,進(jìn)行檢查,以確保這是一個文件,而不是一個目錄。
   if (is_file ($thedir . "/" . $scanarray[$i])){
    //現(xiàn)在,因?yàn)槲覀儗⒃试S客戶端編輯這個文件,
    //我們必須檢查它是否是可讀和可寫。
    if (is_writable ($thedir. "/" . $scanarray[$i]) &&  is_readable($thedir . "/" . $scanarray[$i])){
     //現(xiàn)在,我們檢查文件類型是否存在于允許的類型數(shù)組中.
     $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
     if (in_array ($thepath['extension'], $allowedtypes)){
      //如果文件符合規(guī)定,我們可以繼續(xù)輸出.
      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))){
        //不統(tǒng)計(jì)目錄中包含“.”或“..”的情況
        if ($anitem != "." && $anitem != ".."){
          //此時(shí)如果是一個目錄,則縮進(jìn)一點(diǎn)
          //再去遞歸
          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)){
            //此時(shí)輸出文件.
            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();
    //然后開始解析數(shù)組
    //scandir()用“.”和“..”統(tǒng)計(jì)文件導(dǎo)航列表
    //因此作為文件,我們不應(yīng)該列出他們.
    for ($i = 0; $i < count ($scanarray); $i++){
      if ($scanarray[$i] != "." && $scanarray[$i] != ".."){
        //現(xiàn)在,我們檢查,以確保這是一個文件,而不是一個目錄.
        if (is_file ($thedir . "/" . $scanarray[$i])){
          //現(xiàn)在需要做的是循環(huán)數(shù)據(jù)到一個關(guān)聯(lián)數(shù)組.
          $finalarray[$thedir . "/" . $scanarray[$i]] = filemtime ($thedir . "/" . $scanarray[$i]);
        }
      }
    }
    //至此,我們已經(jīng)遍歷了整個數(shù)組,現(xiàn)在需要做的只是asort()它。
    asort ($finalarray);
    return ($finalarray);
  } else {
    echo "對不起,這個目錄不存在.";
  }
}
//然后,我們將函數(shù)指向我們需要查看的目錄.
$sortedarray = sortfilesbydate ("testfolder");
//至此,就可以按照如下形式輸出:
while ($element = each ($sortedarray)){
  echo "File: " . $element['key'] . " was last modified: " . date ("F j, Y h:i:s", $element['value']) . "<br />";
}
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP目錄操作技巧匯總》、《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論